简体   繁体   English

在Python中使用Lambda +过滤器进行列表过滤

[英]List filtering using lambda + filter in Python

I am finding hard myself to filter a basic need: I have a list and I have to filter it by an value of the items: 我发现自己很难过滤基本需求:我有一个列表,并且必须按项目的值对其进行过滤:
My code: 我的代码:

list1=[{"a":865,"b":1},{"a":56,"b":6},{"a":995,"b":1,"c":5},{"a":1,"b":42}]
a=filter(lambda x:x["b"]==1, list1)
print a

I get the valid output, but if i want to update my filter with a==865 and b==1, i get an invalid syntax error: 我得到有效的输出,但是如果我想用a == 865和b == 1更新我的过滤器,则会收到无效的语法错误:

a=filter(lambda x:x["a"]==865 && x["b"]==1, list1)
print a

Kindly help. 请帮助。 Thanks in advance. 提前致谢。

Replace && with and . 更换&&and That should do it! 那应该做! The docs (python 2) 文档(Python 2)

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

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