I have read that Python does not have built-in support for multi-dimensional arrays-- is this still the case or has this been developed and updated in the last few years for this language?

I have researched this and seen references to multi-dimensional arrays and various work-arounds: dictionaries, lists, and various packages such as NumPy. Honestly, I do not need anything very sophisticated at this point. I suppose dictionaries will work fine (I just need to experiment with it a bit before deciding).

Has anyone here made attempts at simulating a multi-dimensional array, and if so, how did it work out for you?

Thank-you in advance.

Matty D

Sure Python has multidimensional arrays, they are called lists of lists or tuples of tuples etc.

mlist1 = [
[7, 12, 23],
[22, 31, 9],
[4, 17, 31]]

print mlist1  # [[7, 12, 23], [22, 31, 9], [4, 17, 31]]

# show list_item at index 1
print mlist1[1]  # [22, 31, 9]

# show item 2 in that sublist
print mlist1[1][2]  # 9

# change the value
mlist1[1][2] = 99

print mlist1  # [[7, 12, 23], [22, 31, 99], [4, 17, 31]]
commented: help with multi-d arrays +3

Sure Python has multidimensional arrays, they are called lists of lists or tuples of tuples etc.

mlist1 = [
[7, 12, 23],
[22, 31, 9],
[4, 17, 31]]

print mlist1  # [[7, 12, 23], [22, 31, 9], [4, 17, 31]]

# show list_item at index 1
print mlist1[1]  # [22, 31, 9]

# show item 2 in that sublist
print mlist1[1][2]  # 9

# change the value
mlist1[1][2] = 99

print mlist1  # [[7, 12, 23], [22, 31, 99], [4, 17, 31]]

Oh, I see. Thanks so much for your help! ;)

Matty D

I was just playing around with a matrix the other day and found out this:

# create a 10 x 10 matrix of zeroes
matrix10x10 = [[0 for col in range(10)] for row in range(10)]
 
# fill it with 1 diagonally
for i in range(10):
    matrix10x10[i][i] = 1
 
# show it
for row in matrix10x10:
    print row
 
"""
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
"""

The difficulty comes in trying to make your multi-d array into a class with useful methods like __add__(), __mul__(), det(), eigenvalues(), etc.

When I was much fresher at Python, I tried creating a vector class that inherited from the tuple class. The attempt failed, and I moved on to other projects.

Anyone have success in such matters?

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.