Multi-dimensional Arrays:

Reply

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Multi-dimensional Arrays:

 
0
  #1
Feb 19th, 2007
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)
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Multi-dimensional Arrays:

 
1
  #2
Feb 19th, 2007
Sure Python has multidimensional arrays, they are called lists of lists or tuples of tuples etc.
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Multi-dimensional Arrays:

 
0
  #3
Feb 19th, 2007
Originally Posted by bumsfeld View Post
Sure Python has multidimensional arrays, they are called lists of lists or tuples of tuples etc.
  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Multi-dimensional Arrays:

 
0
  #4
Feb 20th, 2007
I was just playing around with a matrix the other day and found out this:
  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. """
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Multi-dimensional Arrays:

 
0
  #5
Feb 22nd, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC