程序
ini=[1,2,3,4,5,6,7,8,9]
count=0
for i in range(2):
for j in range(2):
init[i,j]=ini[count]
count+=1
print(init)
预期产出:
[[1,2],[3,4]]
答案 0 :(得分:0)
请注意,在基本的python中,你不能init[i,j]
,而是int[i][j]
。
如何在嵌套列表推导中使用itertools.count
:
import itertools
c = itertools.count(1)
# or: c = iter([1,2,3,4,5,6]) if data can be anything
result = [[next(c) for _ in range(0,2)] for _ in range(0,2)]
print(result)
结果:
[[1, 2], [3, 4]]
答案 1 :(得分:0)
import itertools
ini=[1,25,36,7,5,6,7,8,9]
count=0
c=itertools.count(0)
init= [[ini[next(c)]for i in range(2)]for j in range(2)]
print(init)