是什么使元素有资格使用Python进行设置的成员资格测试? [英] What makes an element eligible for a set membership test in Python?

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

问题描述

我想了解可以使用Python中的 set 成员资格测试哪些项目。通常,集合成员资格测试的工作方式类似于Python中的 list 成员资格测试。

 >>> {1,2,3}中的1美元

>> {1,2,3}中的0美元
错误
>>

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

列表,好的:

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

设置,因为无法哈希​​,所以不起作用:

 >> {1,2,{1,2}} 
追溯(最近一次呼叫最近):
文件< stdin>,在< module>中的第1行。
TypeError:不可散列的类型:设置
>>>

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

 >> {1,2,3}中的{1} 
错误
>>> {1,2,3}中的{1,2}

>> {1,2,3}中的set()
False
>>>

但是,如果我尝试执行相同的测试,而被测元素为 dict ,我收到一条错误消息,表明所测试的元素不能为散列。

  >>> {1,2} 
追溯中的{’a’:1}(最近一次通话最近):
文件< stdin>,在< module>中的第1行。
TypeError:不可散列的类型: dict
>>> {1,2}中的{}}
追溯(最近一次调用):
文件< stdin>,< module>中的第1行。
TypeError:不可散列的类型: dict
>>>

这不可能是完整的故事,因为设置了 可以测试另一组中的成员资格,即使它本身不能散列,也可以给出结果而不是错误。



所以这个问题是:是什么使元素有资格在Python中进行设置的成员资格测试?

解决方案

您无法测试集中中非哈希元素的成员资格。示例-

 >> {1,2} 
追溯中的[1,2](最近一次调用为最新):
文件< stdin>,< module>中的第1行。
TypeError:不可散列的类型:列出
>>> {1,2}中的{1:2}
追溯(最近一次调用为最新):
文件< stdin>,< module>中的第1行。
TypeError:不可哈希类型:'dict'

唯一可以作为对象的不可哈希对象设置用于遏制检查。如文档-


注意, __ contains __(),remove()和discard()方法的elem参数可以是一个集合。为了支持搜索等效的冻结集,在搜索过程中临时更改元素集,然后将其还原。在搜索过程中,不应读取或突变elem集,因为它没有有意义的值。


支持搜索冻结集与集合具有相同的元素,集合会暂时突变为 frozenset()并进行比较。示例-

 >> {1,2,frozenset([1,2])}中的set([1,2])


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.

List, okay:

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

Set, does not work because unhashable:

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

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

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.

So the question is: What makes an element eligible for a set membership test in Python?

解决方案

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 -

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.

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进行设置的成员资格测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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