是什么让一个元素有资格在 Python 中进行集合成员资格测试?

What makes an element eligible for a set membership test in Python?(是什么让一个元素有资格在 Python 中进行集合成员资格测试?)

本文介绍了是什么让一个元素有资格在 Python 中进行集合成员资格测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解可以在 Python 中测试哪些项目的 set 成员资格.一般来说,集合成员资格测试的工作方式类似于 Python 中的 list 成员资格测试.

I would like to understand which items can be tested for set membership in Python. In general, set membership testing works like list membership testing in Python.

>>> 1 in {1,2,3}
True
>>> 0 in {1,2,3}
False
>>> 

但是,集合与列表的不同之处在于它们不能包含不可散列的对象,例如嵌套集合.

However, sets are different from lists in that they cannot contain unhashable objects, for example nested sets.

列表,好的:

>>> [1,2,{1,2}]
[1, 2, {1, 2}]
>>> 

设置,因为不可散列而不起作用:

>>> {1,2,{1,2}}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
>>> 

现在,即使集合不能是其他集合的成员,我们也可以在成员资格测试中使用它们.这样的检查不会导致错误.

Now, even if sets cannot be members of other sets, we can use them in membership tests. Such a check does not result in an error.

>>> {1} in {1,2,3}
False
>>> {1,2} in {1,2,3}
False
>>> set() in {1,2,3}
False
>>> 

但是,如果我尝试在被测试的元素是 dict 的情况下进行相同的测试,我会收到一个错误,表明被测试的元素不能是不可散列的.

However, if I try to do the same test where the element being tested is a dict, I get an error which suggests that the element being tested cannot be unhashable.

>>> {'a':1} in {1,2}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> {} in {1,2}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> 

这不可能是全部,因为一个 set 可以被测试是否属于另一个集合,即使它本身是不可散列的,给出结果而不是错误.

That cannot be the whole story, because a set can be tested for membership in another set even if it is itself unhashable, giving a result rather than an error.

所以问题是:是什么让一个元素有资格在 Python 中进行集合成员资格测试?

推荐答案

您无法测试 set 中不可散列元素的成员资格.示例 -

You cannot test membership of non-hashable elements in a set . Example -

>>> [1,2] in {1,2}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> {1:2} in {1,2}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'

设置了唯一可用于包含检查的不可散列对象.如 文档中所述 -

The only non-hashable object that can be used for containment checking is set. As given in the documentation -

注意,__contains__()、remove() 和 discard() 方法的 elem 参数可能是一个集合.为了支持对等价的frozenset 的搜索,elem 集在搜索过程中会暂时发生变异,然后再恢复.在搜索过程中,不应该读取或改变元素集,因为它没有有意义的值.

Note, the elem argument to the __contains__(), remove(), and discard() methods may be a set. To support searching for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the elem set should not be read or mutated since it does not have a meaningful value.

为了支持搜索具有与集合相同元素的frozensets,将集合临时变异为frozenset() 并进行比较.示例 -

To support searching for frozensets with same elements as a set, a set is temporarily mutated to frozenset() and compared. Example -

>>> set([1,2]) in {1,2,frozenset([1,2])}
True

这篇关于是什么让一个元素有资格在 Python 中进行集合成员资格测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是什么让一个元素有资格在 Python 中进行集合成员资格测试?

基础教程推荐