943,697 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 13682
  • Python RSS
Dec 3rd, 2008
0

Empty 2D Array

Expand Post »
Hello all.

I want to craete a empty double dimensional array.
Later i will get the row and column length. But row length varies each time program. But i will be having the lenght of the row. So hoe to write a generic code for creating a empty 2D array and dynamically insert values in it.

ASAP
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Nanz is offline Offline
1 posts
since Dec 2008
Dec 3rd, 2008
1

Re: Empty 2D Array

Click to Expand / Collapse  Quote originally posted by Nanz ...
hoe to write a generic code for creating a empty 2D array and dynamically insert values in it.
python Syntax (Toggle Plain Text)
  1. >>> arr_2d = [[]]
  2. >>> arr_2d[0].append(0)
  3. >>> arr_2d[0].append(1)
  4. >>> arr_2d.append([3,4,5])
  5. >>> arr_2d
  6. [[0, 1], [3, 4, 5]]
  7. >>>

It might benefit you to create a 2d array class to handle the many different aspects of working with a 2d array.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Dec 3rd, 2008
0

Re: Empty 2D Array

Not sure if I follow this correctly, but you start with a one dimensional list and append other lists to it. For example ...
python Syntax (Toggle Plain Text)
  1. qq = []
  2. for row in range(3):
  3. for col in range(3):
  4. qq.append([row, col])
  5.  
  6. print qq
  7.  
  8. """
  9. my output -->
  10. [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]]
  11. """
You could use None as a placeholder.
Last edited by vegaseat; Dec 3rd, 2008 at 11:25 am.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Dec 3rd, 2008
0

Re: Empty 2D Array

I like using list comprehensions for this kind of task.
Python Syntax (Toggle Plain Text)
  1. >>> cols = 4
  2. >>> rows = 5
  3. >>> array = [[0 for i in range(cols)] for j in range(rows)]
  4. >>> array
  5. [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
  6. >>>
Reputation Points: 86
Solved Threads: 40
Junior Poster
solsteel is offline Offline
141 posts
since Mar 2007
Dec 3rd, 2008
0

Re: Empty 2D Array

If it is easier to get your head around, you can use a dictionary of lists, with the dictionary key as the row number, each containing x columns. You could also take this one step further and use a dictionary of dictionaries with row and column number as keys.
Python Syntax (Toggle Plain Text)
  1. def populate_array(rows, columns):
  2. array_dic = {}
  3. for row in range(1, rows+1): ## starts with one, not zero
  4. array_dic[row] = []
  5. for col in range(0, columns):
  6. array_dic[row].append('None') ## initialize to 'None'
  7. return array_dic
  8.  
  9. if __name__ == "__main__":
  10. rows = 10
  11. columns = 5
  12. ret_dic = populate_array(rows, columns)
  13. for key in ret_dic.keys():
  14. print key, ret_dic[key]
  15.  
  16. ##--- change some value
  17. row = 3
  18. col = 1
  19. ret_dic[row][col] = 'Changed'
  20. print "\n"
  21. for key in ret_dic.keys():
  22. print key, ret_dic[key]
Last edited by woooee; Dec 3rd, 2008 at 5:43 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,305 posts
since Dec 2006
Dec 3rd, 2008
0

Re: Empty 2D Array

Click to Expand / Collapse  Quote originally posted by woooee ...
You could also take this one step further and use a dictionary of dictionaries with row and column number as keys.
Brilliant. I really like this idea! Nice!
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: python-ldap and cached passwords
Next Thread in Python Forum Timeline: python and xitami





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


Follow us on Twitter


© 2011 DaniWeb® LLC