简体   繁体   English

我如何根据特定标准对列表进行排序

[英]How can i sort a list from specifik criteria

I have this list and I want to sort the list.我有这个列表,我想对列表进行排序。 This is just a smaller example of what I want to do, but I get the same error.这只是我想做的一个较小的例子,但我得到了同样的错误。 I dont understand why I can't make this work.我不明白为什么我不能完成这项工作。 I have tried using google to solve the problem but without luck.我曾尝试使用谷歌来解决问题但没有运气。

lst = [3, 4, 5, 6]

if lst < 4:
    lst.pop()
    print(lst)

How can i do this it shows我该怎么做它显示

TypeError:'<' not supported between instances of 'list' and 'in TypeError: '<' 在 'list' 和 'in 的实例之间不支持

I think that your goal is to remove all elements in the list that are lesser than 4. You can use this simple list comprehension in order to achieve what you want:我认为你的目标是删除列表中小于 4 的所有元素。你可以使用这个简单的list comprehension来实现你想要的:

lst = [3, 4, 5, 6]
lst = [elem for elem in lst if elem >= 4]
print(lst)

Output: Output:

[4, 5, 6]

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

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