Если вы уже определили свои объекты, списочные списки могут помочь здесь:
num_rows = 5
num_cols = 6
row = [Cell()]*num_rows
# The original way doesn't behave exactly right, this avoids
# deep nesting of the array. Also adding list(row) to create
# a new object rather than carrying references to row to all rows
mat = [list(row) for i in range(num_cols)]
#[[Cell(), Cell(), Cell()...], [...], ..., [Cell(), ..., Cell()]]
Вы можете обернуть их в numpy.array
, если хотите, также
Массив NumPy заполнен
Вы также можете использовать встроенный метод numPy full
, который генерирует массив NumPy n
by m
, заполненный вашими значениями:
mat = numpy.full((num_rows, num_cols), Cell())
Документацию можно найти здесь