我有一个对象列表,比如说foos。
我有一个用于创建新列表的循环。
foo1 = {id:1,location:2}
例如:foos = [foo1, foo2, foo3]
现在我想根据位置创建一个新列表。
new_list = []
for foo in foos:
if foo.location==2:
new_list.append(foo)
我想知道是否有一种方法可以像这样做:
new_list = []
new_list = map(if foo.location ==2,foos) // this is wrong code but is something like this possible. ?
我可以在这里使用map函数吗?如果可以,应该如何使用?
(请注意:保留了原始HTML标记。)