过滤集合的最 Pythonic 方式是什么?
问题描述
我有一个包含替换的列表,我想做两件事:
I have list consisting with replacements and I want to do two things:
- 删除重复项
- 按特定条件删除所有元素,确切地说,我想删除所有大于某个值的元素.
我想我可以使用过滤器 2 而不是使用设置来实现 1像
I figured I can use filter for 2 and than use set to achieve 1 something like
list(set(filter(lambda x:x<C, l)))
有没有更好/更pythonic/更有效的方法?
is there a better/more pythonic/more efficient way?
解决方案
使用 列表推导 可能更pythonic".
Using list comprehension is maybe more "pythonic".
filtered = [x for x in set(lst) if x < C]
相关文章