我的输出错误[[1, 3, 2, 0, 0], [3, 2, 0, 0], [2, 0, 0], [0, 0], [0]]
任何人都可以解决它,我被困在这里了吗?
if __name__=="__main__":
row = [[4, 6, 5, 3, 3], [2, 4, 3, 1, 1], [4, 6, 5, 3, 3], [3, 5, 4, 2, 2], [1, 3, 2, 0, 0], [1, 3, 2, 0, 0]]
for x in row:
diff2 = [(x[z:]) for z in range(len(row)-1)]
print(diff2)
输出
[[4, 6, 5, 3, 3], [4, 3, 1, 1], [5, 3, 3], [2, 2], [0]]
答案 0 :(得分:3)
如果我理解你想要的东西:
>>> print([r[i:] for i, r in enumerate(row)])
[[4, 6, 5, 3, 3], [4, 3, 1, 1], [5, 3, 3], [2, 2], [0], []]
或者如果您不想要最后一个列表
>>> print([r[i:] for i, r in enumerate(row[:-1])])
[[4, 6, 5, 3, 3], [4, 3, 1, 1], [5, 3, 3], [2, 2], [0]]
如果您想要为切碎列表排序的实际元素:
>>> print([sorted(r[i:]) for i, r in enumerate(row[:-1])])
[[3, 3, 4, 5, 6], [1, 1, 3, 4], [3, 3, 5], [2, 2], [0]]
排序排序:
>>> print(sorted([sorted(r[i:]) for i, r in enumerate(row[:-1])]))
[[0], [1, 1, 3, 4], [2, 2], [3, 3, 4, 5, 6], [3, 3, 5]]