[英]Unable to get all the values inside of a 2D array
grid = []
for i in range(int(rows)):
row = []
for n in range(int(columns)):
row.append(int(contents[n]))
grid.append(row)
print(grid)
The contents array has 9 values inside of it, all of them being strings.内容数组里面有 9 个值,它们都是字符串。 How do I make it so that I get all the 9 values of the contents array inside the grid, so that each value would be present in the grid array, rather than currently just having the first 3 values in 3 rows.
我如何做到这一点,以便我在网格内获取内容数组的所有 9 个值,以便每个值都将出现在网格数组中,而不是目前仅在 3 行中包含前 3 个值。
Use a separate variable that increments separately from the for loops, instead of using n
to grab the values from the list.使用与 for 循环分开递增的单独变量,而不是使用
n
从列表中获取值。 So, initialize some variable to be 0, like m = 0
outside of the first for loop, then every time you append contents[m]
, you would increment m
by one.因此,将一些变量初始化为 0,例如在第一个 for 循环之外的
m = 0
,然后每次 append contents[m]
时,都会将m
加一。
I'm going to use j
instead of your n
, and n
instead of m
in this example:在此示例中,我将使用
j
代替您的n
,并使用n
代替m
:
n = 0
grid = []
for i in range(int(rows)):
row = []
for j in range(int(columns)):
row.append(int(contents[n]))
n += 1
grid.append(row)
print(grid)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.