简体   繁体   中英

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=filter(lambda x:x["a"]==865 && x["b"]==1, list1)
print a

Kindly help. Thanks in advance.

Replace && with and . That should do it! The docs (python 2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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