1,105,546 Community Members

Multi-dimensional Arrays:

Member Avatar
mattyd
Posting Maven
2,677 posts since Oct 2006
Reputation Points: 76 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
Featured
 
0
 

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

Member Avatar
bumsfeld
Posting Virtuoso
1,537 posts since Jul 2005
Reputation Points: 399 [?]
Q&As Helped to Solve: 261 [?]
Skill Endorsements: 7 [?]
 
1
 

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]]
Member Avatar
mattyd
Posting Maven
2,677 posts since Oct 2006
Reputation Points: 76 [?]
Q&As Helped to Solve: 7 [?]
Skill Endorsements: 0 [?]
Featured
 
0
 

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

Member Avatar
sneekula
Nearly a Posting Maven
2,496 posts since Oct 2006
Reputation Points: 917 [?]
Q&As Helped to Solve: 263 [?]
Skill Endorsements: 5 [?]
 
0
 

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]
"""
Member Avatar
jrcagle
Practically a Master Poster
608 posts since Jul 2006
Reputation Points: 77 [?]
Q&As Helped to Solve: 160 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article