简体   繁体   English

带过滤器的Lambda函数

[英]Lambda function with filter

Only include products that are non-discontinued (discontinued=0) with unit_price greater than 15.0 in the new list. 在新列表中仅包括单价大于15.0且已停产(停产= 0)的产品。 How I can solve this? 我该如何解决? How can I select and use discontinued=0 with unit_price and put it into the code? 如何选择和使用unit_price的continuous discontinued=0并将其放入代码中?

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: 我想象产品要么是某个Product类的对象,要么是字典,如果是这样,您可以使用:

For objects of a certain class (having discontinued and unit_price attributes): 对于特定类别的对象(具有discontinued属性和unit_price属性):

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

For dicts (having discontinued and unit_price keys): 对于字典(具有discontinued键和unit_price键):

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

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

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