我正在使用 Python 中的列表进行操作。
In [52]: myList = [1,2,3,4,5]
In [54]: c=[[]]*10
In [55]: for i, elem1 in enumerate(myList):
....: b = [elem1 + elem2 for elem2 in range(10)]
....: minSum, minSumIndex= min((val, idx) for (idx, val) in enumerate(b))
....: c[minSumIndex].append(elem1)
我希望将所有元素myList
附加到c[0]
,因为elem1+1
总是给出最小的总和。
print c[0]
[1, 2, 3, 4, 5]
print c[1]
[]
print c[2]
[]
...
但是,我最终得到了这个:
In [56]: shape(c)
Out[56]: (10, 5)
In [57]: print c[0]
[1, 2, 3, 4, 5]
In [58]: print c[1]
[1, 2, 3, 4, 5]
In [59]: print c[2]
[1, 2, 3, 4, 5]
In [60]: print c[3]
[1, 2, 3, 4, 5]
In [61]: print c[4]
[1, 2, 3, 4, 5]
In [62]: print c[5]
[1, 2, 3, 4, 5]
哪里出错了?