嘿家伙试图完成我的程序。这是我的代码:
lists = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
#I want to make a new list consisting of only numbers above 50 from that list
if any(list > 50 for list in list):
newlists = list
我不知道该怎么做。我做错了什么,有人可以帮助我吗?
嘿家伙试图完成我的程序。这是我的代码:
lists = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
#I want to make a new list consisting of only numbers above 50 from that list
if any(list > 50 for list in list):
newlists = list
我不知道该怎么做。我做错了什么,有人可以帮助我吗?
newlist = [x for x in lists if x > 50]
在此处阅读有关列表推导的信息
两种选择。使用列表推导:
lst = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[x for x in lst if x > 50]
并filter
在 Python 2.x 中使用:
filter(lambda x: x > 50, lst)
或者filter
在 Python 3.x 中使用,如注释中所指出的,filter
在此版本中返回一个迭代器,如果需要,需要先将结果转换为列表:
list(filter(lambda x: x > 50, lst))
无论如何,结果如预期:
=> [60, 70, 80, 90, 100]