繁体   English   中英

在 python 的每个列表列表中移动特定值(基于该值)

[英]move specific values (based on that value) in each list of lists in python

我有一个看起来像这样的列表:

list= [[0,1,0,0,0][0,0,0,2,0],...,[0,0,10,0,0]]

作为 output,我希望将所有值 (1,2..,10) 移到它们所在列表的开头,例如:

list= [[1,0,0,0,0][2,0,0,0,0],...,[10,0,0,0,0]]

我努力了:

new_list= list.insert(0, list.pop(list.index(value)))

这适用于一个列表,但我想对列表中的所有列表都这样做。

您可以排序:

l = [[0,1,0,0,0],[0,0,0,2,0],[0,0,10,0,0]]
print([sorted(i, reverse=True) for i in l])

Output:

[[1, 0, 0, 0, 0], [2, 0, 0, 0, 0], [10, 0, 0, 0, 0]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM