in C ++ I can write:
int someArray[8][8]; for (int i=0; i < 7; i++) for (int j=0; j < 7; j++) someArray[i][j] = 0;
And how can I initialize multi-line arrays in python? I tried:
array = [[],[]] for i in xrange(8): for j in xrange(8): array[i][j] = 0
>>> [[0]*8 for x in xrange(8)] [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] >>>
You asked about initializing a list of lists. This is a very useful data structure, but it has an important difference from a 2D array in C ++: there is no guarantee that all lines are the same length (i.e. What len(a[0])==len(a[1])(while in C ++ you have this guarantee).
len(a[0])==len(a[1])
So, another solution that may be convenient uses a NumPy array datatype, for example:
import numpy as np array = np.zeros((8,8))
:
array = [] for i in xrange(8): array.append( [0] * 8 )
array = [[0]*8 for i in xrange(8)]
[[0]*8 for x in range(8)]