944,113 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 86029
  • Python RSS
Feb 19th, 2007
0

Multi-dimensional Arrays:

Expand Post »
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
Last edited by mattyd; Feb 19th, 2007 at 2:42 pm. Reason: (sp)
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Feb 19th, 2007
1

Re: Multi-dimensional Arrays:

Sure Python has multidimensional arrays, they are called lists of lists or tuples of tuples etc.
python Syntax (Toggle Plain Text)
  1. mlist1 = [
  2. [7, 12, 23],
  3. [22, 31, 9],
  4. [4, 17, 31]]
  5.  
  6. print mlist1 # [[7, 12, 23], [22, 31, 9], [4, 17, 31]]
  7.  
  8. # show list_item at index 1
  9. print mlist1[1] # [22, 31, 9]
  10.  
  11. # show item 2 in that sublist
  12. print mlist1[1][2] # 9
  13.  
  14. # change the value
  15. mlist1[1][2] = 99
  16.  
  17. print mlist1 # [[7, 12, 23], [22, 31, 99], [4, 17, 31]]
Last edited by bumsfeld; Feb 19th, 2007 at 3:10 pm. Reason: spells
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Feb 19th, 2007
0

Re: Multi-dimensional Arrays:

Click to Expand / Collapse  Quote originally posted by bumsfeld ...
Sure Python has multidimensional arrays, they are called lists of lists or tuples of tuples etc.
python Syntax (Toggle Plain Text)
  1. mlist1 = [
  2. [7, 12, 23],
  3. [22, 31, 9],
  4. [4, 17, 31]]
  5.  
  6. print mlist1 # [[7, 12, 23], [22, 31, 9], [4, 17, 31]]
  7.  
  8. # show list_item at index 1
  9. print mlist1[1] # [22, 31, 9]
  10.  
  11. # show item 2 in that sublist
  12. print mlist1[1][2] # 9
  13.  
  14. # change the value
  15. mlist1[1][2] = 99
  16.  
  17. print mlist1 # [[7, 12, 23], [22, 31, 99], [4, 17, 31]]
Oh, I see. Thanks so much for your help!

Matty D
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Feb 20th, 2007
0

Re: Multi-dimensional Arrays:

I was just playing around with a matrix the other day and found out this:
python Syntax (Toggle Plain Text)
  1. # create a 10 x 10 matrix of zeroes
  2. matrix10x10 = [[0 for col in range(10)] for row in range(10)]
  3.  
  4. # fill it with 1 diagonally
  5. for i in range(10):
  6. matrix10x10[i][i] = 1
  7.  
  8. # show it
  9. for row in matrix10x10:
  10. print row
  11.  
  12. """
  13. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  14. [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
  15. [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
  16. [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
  17. [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  18. [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
  19. [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
  20. [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
  21. [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
  22. [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  23. """
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Feb 22nd, 2007
0

Re: Multi-dimensional Arrays:

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
Last edited by jrcagle; Feb 22nd, 2007 at 11:22 pm.
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: old-style class
Next Thread in Python Forum Timeline: Play Flash Files with wxPython





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC