过滤集合的最 Pythonic 方式是什么?

What is the most Pythonic way to filter a set?(过滤集合的最 Pythonic 方式是什么?)

本文介绍了过滤集合的最 Pythonic 方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含替换的列表,我想做两件事:

I have list consisting with replacements and I want to do two things:

  1. 删除重复项
  2. 按特定条件删除所有元素,确切地说,我想删除所有大于某个值的元素.

我想我可以使用过滤器 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]

这篇关于过滤集合的最 Pythonic 方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:过滤集合的最 Pythonic 方式是什么?

基础教程推荐