简体   繁体   English

从嵌套列表 [[name1,score],[name2,score],...] 中,如何删除分数最低的名称?

[英]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.

相关问题 如何使用转义在Python中用输入字符串替换name1和name2? - How do I replace name1 and name2 with an input string in Python using escaping? 显示与该分数关联的列表中的最高分数和名称 - Show highest score and name from list associated with that score 使用循环从列表中查找最高分、最低分 - Using loops to find the highest score, lowest score from a list 如何在 Python 中分隔名称和分数列表? - How to separate a-name-and-score-list in Python? 我有一个包含图形边缘的 .csv 文件(Name1、Name2)。 如何将其转换为邻接矩阵? - I have a .csv file that contains the edges of a graph (Name1, Name2). How do I convert this into a adjacency matrix? 如何编写用户定义的 function 来计算 NAME 中的名字并与分数列中的名字完全匹配分数? - How do I write a user defined function to count the first name in the NAME and exactly match the score with respect to first name in the score column? 多项试验中得分最低 - Lowest score from multiple trials NameError:名称“分数”未定义 - NameError: name 'score' is not defined 从用户输入的列表中删除名称,并平均成绩 - deleting a name from a list input by user and averaging a class score Python-如何输出得分最高的球员及其姓名 - Python - How to output the player with the highest score and their name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM