Talk:Creating an Array: Difference between revisions

(Python, and Dim multidimentional arrays)
Line 32:
Is there any easy way to do a Dim (like from Basic) on Python, as a multidimentional array?
'a=[0]*8' seems to be similar to 'dim a[8]', but i have no idea about how to do on an at least bidimentional array, since neither 'a=[0][0]*8' nor 'a=[0]*8[0]*8' works...
 
:Here are two ways to do two dim arrays in python:
 
>>> m,n = 3,4
>>> from pprint import pprint as pp
>>> a = dict(((x,y), 0) for x in range(m) for y in range(n))
>>> pp(a)
{(0, 0): 0,
(0, 1): 0,
(0, 2): 0,
(0, 3): 0,
(1, 0): 0,
(1, 1): 0,
(1, 2): 0,
(1, 3): 0,
(2, 0): 0,
(2, 1): 0,
(2, 2): 0,
(2, 3): 0}
>>> a[(0,0)] = 1
>>> a[(3,2)] = 1
>>> pp(a)
{(0, 0): 1,
(0, 1): 0,
(0, 2): 0,
(0, 3): 0,
(1, 0): 0,
(1, 1): 0,
(1, 2): 0,
(1, 3): 0,
(2, 0): 0,
(2, 1): 0,
(2, 2): 0,
(2, 3): 0,
(3, 2): 1}
>>> b = [[0]*m for _ in range(n)]
>>> pp(b)
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> b[0][0] = 1
>>> b[3][2] = 1
>>> pp(b)
[[1, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 1]]
>>>
 
== Merge Create, Assign, and Retreive from Array ==
Anonymous user