假设我有两个这样的列表,
Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]]
ci = [11,5,3,5,5,4]
现在,我要列出另一个m x n的列表(I),它看起来像这样,
I =
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[11, 0, 0, 0, 5, 0, 0, 3, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 4, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我尝试了这段代码,
m,n = 8,10
I = [[0 for j in range(n)] for i in range(m)]
for i, j in Ii:
I[m - i][j - 1] = 1
哪个输出看起来像这样
I =
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 1, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
现在我有1个想要ci值的位置。但是我想将ci的值放在1的位置。需要有关如何执行此操作的帮助吗?
答案 0 :(得分:1)
m = 8
n = 10
Ii = [[7,1],[7,5],[7,8],[5,8],[2,8],[3,5]]
ci = [11,5,3,5,5,4]
I = [[0 for j in range(n)] for i in range(m)]
for z, *j, in enumerate(Ii):
for i, k in j:
I[m-i][k-1] = ci[z]
for i in I:
print(i)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[11, 0, 0, 0, 5, 0, 0, 3, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 4, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 5, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]