[英]From nested list [[name1,score],[name2,score],...], how do I remove the names with the lowest score?
I have a mixed nested list [[name1,score],[name2,score],...]
.我有一个混合嵌套列表[[name1,score],[name2,score],...]
。 How do I remove the names with the lowest score?如何删除分数最低的名字? I selected an element with the lowest score and looped through the list and compared scores and remove if the score matches the min.我选择了一个分数最低的元素并循环遍历列表并比较分数并在分数与最小值匹配时删除。
lmin=min(list, key = lambda x: x[1])
for l in list:
if( ls[1] == lmin1[1]):
ls1.remove(l)
But it doesn't remove all, only the first instance.但它不会删除所有内容,只会删除第一个实例。 Where am I making a mistake?我在哪里犯了错误? Best,最好的事物,
This is a better way to filter:这是一种更好的过滤方式:
lmin=min(list_, key = lambda x: x[1])
li = [ item for item in list_ if item[1] != lmin]
Note that you shouldnt use the word list
as variable as it is a saved keyword...请注意,您不应将单词list
用作变量,因为它是一个已保存的关键字...
You can do it with:你可以这样做:
Mylist = [["name1",10],["name2",4],["name3",40]]
print(Mylist)
to_remove=min(Mylist, key = lambda x: x[1])
Mylist.remove(to_remove)
print(Mylist)
result:结果:
[['name1', 10], ['name2', 4], ['name3', 40]]
[['name1', 10], ['name3', 40]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.