x = [2, 0, 1, 1, 2, 4]
y = []
for i, k in enumerate(x):
if len(y) - 1 < k: #if our list isn't long enough for this value
while (len(y) - 1 != k):
y.append([]) #make it long enough
y[k].append(i) #append our current index to this values list
print (y)
这是我的快速解决方案
或者使用列表理解:
很简单:
我们列出一个正确数量的列表,然后遍历并将每个索引添加到适当的子列表中。在
相关问题 更多 >
编程相关推荐