| | |
Empty 2D Array
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
hoe to write a generic code for creating a empty 2D array and dynamically insert values in it.
python Syntax (Toggle Plain Text)
>>> arr_2d = [[]] >>> arr_2d[0].append(0) >>> arr_2d[0].append(1) >>> arr_2d.append([3,4,5]) >>> arr_2d [[0, 1], [3, 4, 5]] >>>
It might benefit you to create a 2d array class to handle the many different aspects of working with a 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 ...
You could use None as a placeholder.
python Syntax (Toggle Plain Text)
qq = [] for row in range(3): for col in range(3): qq.append([row, col]) print qq """ my output --> [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]] """
Last edited by vegaseat; Dec 3rd, 2008 at 11:25 am.
May 'the Google' be with you!
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
I like using list comprehensions for this kind of task.
Python Syntax (Toggle Plain Text)
>>> cols = 4 >>> rows = 5 >>> array = [[0 for i in range(cols)] for j in range(rows)] >>> array [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] >>>
•
•
Join Date: Dec 2006
Posts: 1,067
Reputation:
Solved Threads: 299
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)
def populate_array(rows, columns): array_dic = {} for row in range(1, rows+1): ## starts with one, not zero array_dic[row] = [] for col in range(0, columns): array_dic[row].append('None') ## initialize to 'None' return array_dic if __name__ == "__main__": rows = 10 columns = 5 ret_dic = populate_array(rows, columns) for key in ret_dic.keys(): print key, ret_dic[key] ##--- change some value row = 3 col = 1 ret_dic[row][col] = 'Changed' print "\n" for key in ret_dic.keys(): print key, ret_dic[key]
Last edited by woooee; Dec 3rd, 2008 at 5:43 pm.
![]() |
Similar Threads
- Using recursion to find largest item in an array (C++)
- checkbox array storage and retrieval (PHP)
- Empty Arrays (C++)
- Read_One_Value Array (C++)
- Variable passed to each() is not an array (PHP)
- 2-D array (C++)
- HOW TO PROGRAM C++ homework (C++)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Python Forum
- Previous Thread: python-ldap and cached passwords
- Next Thread: python and xitami
Views: 2738 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied alarm aliased basic beginner casino character code corners cursor definedlines development dictionary digital dynamic editing error events examples excel exe file filename float format ftp function graphics gui handling homework ideas iframe import input java line linux list lists logging loop matching mouse newb number numbers numeric output parameters parsing path port prime program programming progressbar projects py py2exe pygame pyglet pyqt pysimplewizard python random recursion recursive return reverse schedule scrolledtext searchingfile shebang skinning sprite ssh statistics stdout string strings table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable voip windows wxpython






