Answer the question
In order to leave comments, you need to log in
[[+content_image]]
Python creating and populating a 2D array
I'm new to python and it's not clear that when creating an array in this way: b = [[0] * 2] * 2
And filling:
for i in range(w):
for r in range(l):
print(i,r)
b[i][r] = str(i) + str(r)
Answer the question
In order to leave comments, you need to log in
Example without numpy, no external libraries at all
#!/usr/bin/env python3
a = 3
b = 5
r = 0 # Чтобы было, чем заполнять
mas = []
for i in range(a):
mas.append([])
for j in range(b):
mas[i].append(r)
r += 1 # Чтобы заполнялось не одно и тоже
print(mas)
# [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14]]
I will supplement @Bce_OK with
an example:
>>> a = [[0] * 2] * 2
>>> a
[[0, 0], [0, 0]]
>>> a[0][0] = 1
>>> a
[[1, 0], [1, 0]]
>>>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question