过滤集合的最 Pythonic 方法是什么? [英] What is the most Pythonic way to filter a set?

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

问题描述

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

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

我想我可以使用过滤器 2 而不是使用 set 来实现 1类似的东西

list(set(filter(lambda x:x

有更好/更pythonic/更有效的方法吗?

解决方案

使用 列表理解

a> 可能更pythonic".

filtered = [x for x in set(lst) if x 

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

  1. remove duplicates
  2. remove all elements by a specific criteria, to be exact I want to remove all elements bigger than a certain value.

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)))

is there a better/more pythonic/more efficient way?

Using list comprehension is maybe more "pythonic".

filtered = [x for x in set(lst) if x < C]

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆