Empty 2D Array

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 1
Reputation: Nanz is an unknown quantity at this point 
Solved Threads: 0
Nanz Nanz is offline Offline
Newbie Poster

Empty 2D Array

 
0
  #1
Dec 3rd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Empty 2D Array

 
1
  #2
Dec 3rd, 2008
Originally Posted by Nanz View Post
hoe to write a generic code for creating a empty 2D array and dynamically insert values in it.
  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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,137
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Empty 2D Array

 
0
  #3
Dec 3rd, 2008
Not sure if I follow this correctly, but you start with a one dimensional list and append other lists to it. For example ...
  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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 110
Reputation: solsteel is on a distinguished road 
Solved Threads: 31
solsteel solsteel is offline Offline
Junior Poster

Re: Empty 2D Array

 
0
  #4
Dec 3rd, 2008
I like using list comprehensions for this kind of task.
  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. >>>
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,067
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is online now Online
Veteran Poster

Re: Empty 2D Array

 
0
  #5
Dec 3rd, 2008
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Empty 2D Array

 
0
  #6
Dec 3rd, 2008
Originally Posted by woooee View Post
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!
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2738 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC