为什么Python的过滤器(谓词,集合)不返回集合? [英] Why doesn't Python's filter(predicate, set) return a set?

查看:81
本文介绍了为什么Python的过滤器(谓词,集合)不返回集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Python的filter设计为如果您运行filter(my_predicate, some_set),我得到的list对象返回要比set对象返回?
在实际情况下,您会希望结果为set ...吗?

Why was Python's filter designed such that if you run filter(my_predicate, some_set), I get back a list object return than a set object?
Are there practical cases where you would not want the result to be a set...?

推荐答案

您可以进行设定的理解.

You can do a set comprehension.

{my_predicate(x) for x in some_set}      # mapping
{x for x in some_set if my_predicate(x)} # filtering

例如

In [1]: s = set([1,2,3])

In [2]: {x%2 for x in s}
Out[2]: {0, 1}

Python 2 中的许多功能"函数都以将list作为输出类型进行了标准化.这只是很久以前的API选择.在itertools中,许多相同的功能"函数都标准化了提供生成器的功能,您可以从该生成器中填充所需的任何数据结构.并且在Python 3中,它们在提供迭代器方面是标准化的.

Many of the "functional" functions in Python 2 are standardized on having list as the output type. This was just an API choice long ago. In itertools many of the same "functional" functions standardize on providing a generator from which you could populate whatever data structure you'd like. And in Python 3 they are standardized on providing an iterator.

但是还要注意,Python中的过滤"与其他语言中的过滤"不同,例如Haskell.它不被认为是数据结构上下文中的一种转换,并且您不会选择通过使它们成为Functor实例(或其他任何实例)来赋予"数据结构可过滤性".其他语言也有类似的想法.

But do also note that "filtering" in Python is not like it is in some other languages, like, say Haskell. It's not considered to be a transformation within the context of the data structure, and you don't choose to "endow" your data structures with "filterability" by making them an instance of Functor (or whatever other similar ideas exist in other languages).

因此,这是Python中的一个常见用例,它说:这里是一个集合,但是我只想返回所有小于5的值.在此之后,我不在乎它们的'设置性'这就是我要对它们做其他工作的原因,所以请给我一个____."无需为保存值最初存在的上下文而发疯.

As a result, it's a common use case in Python to say something like: "Here's a set, but I just want back all of the values less than 5. I don't care about their 'set-ness' after that point cause I'm just going to do some other work on them, so just give me a ____." No need to get all crazy about preserving the context within which the values originally lived.

在动态打字文化中,这是非常合理的.但是在静态类型化文化中,转换期间保留类型可能很重要,这会让人感到沮丧.从Python的特定角度来看,这实际上只是一种启发.

In a dynamic typing culture this is very reasonable. But in a static typing culture where preserving the type during transformations might matter, this would be a bit frustrating. It's really just sort of a heuristic from Python's particular perspective.

如果它真的只是在settuple的非常狭窄的上下文中,那么我可能只写一个辅助函数:

If it was really just in a very narrow context of a set or tuple then I might just write a helper function:

def type_preserving_filter(predicate, data):
    return type(data)(filter(predicate, data))

例如

>>> type_preserving_filter(lambda x: x > 3, set([1,2,3,4,5,6,7,7]))
{4, 5, 6, 7}
>>> type_preserving_filter(lambda x: x > 3, list([1,2,3,4,5,6,7,7]))
[4, 5, 6, 7, 7]
>>> type_preserving_filter(lambda x: x > 3, tuple([1,2,3,4,5,6,7,7]))
(4, 5, 6, 7, 7)

在Python 2.10和Python 3.4中均可使用.在Python 2中,这有点浪费.使用Python 3中的迭代器进行构造会更好.

which works in both Python 2.10 and Python 3.4. In Python 2 this feels a bit wasteful; constructing from the iterator in Python 3 is better.

这篇关于为什么Python的过滤器(谓词,集合)不返回集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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