| | |
Empty 2D Array
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,008
Reputation:
Solved Threads: 285
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
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment avogadro backend beginner binary bluetooth book builtin calculator character code converter countpasswordentry curved customdialog dan08 dictionaries dictionary dynamic examples exe file float format function gnu graphics gui heads homework ideas import inches input java launcher library line lines linux list lists loop mouse mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable wordgame write wxpython xlib






