简体   繁体   中英

Lambda function with filter

Only include products that are non-discontinued (discontinued=0) with unit_price greater than 15.0 in the new list. How I can solve this? How can I select and use discontinued=0 with unit_price and put it into the code?

print (list(filter(lambda x: x=0,>15,products)))

I imagine products are either objects of a certain Product class, or are dicts, if that's the case, you can use:

For objects of a certain class (having discontinued and unit_price attributes):

print(list(filter(lambda x: x.discontinued == 0 and x.unit_price > 15, products)))

For dicts (having discontinued and unit_price keys):

print(list(filter(lambda x: x['discontinued'] == 0 and x['unit_price'] > 15, products)))

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