What makes an element suitable for an established membership test in Python?

I would like to understand what elements can be tested for set membership in Python. In general, setting up membership testing works like list membership testing in Python.

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

However, sets differ from lists in that they cannot contain unpackable objects, such as nested sets.

The list is all right:

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

Install does not work because it does not shake:

 >>> {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 lead to 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 run the same test where the item being tested is dict , I get an error message that suggests that the item being tested cannot be disabled.

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

This may not be the whole story, because set can be tested for membership in another set, even if it does not shake itself, giving a result, not an error.

So the question is: What does an element do for a member set test in Python?

+7
python collections set
source share
2 answers

You cannot verify the ownership of non-hashable elements in 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' 

A single facility has been installed that cannot be used to check for leaks. As stated in the documentation -

Note. The elem argument for __ contains the __ (), remove (), and discard () methods that can be set . To support the search for an equivalent frozenset, the elem set is temporarily mutated during the search and then restored. During the search, the set of elements should not be read or mutated, since it does not have a significant value.

To support the search for freezons with the same elements as the set, the set is temporarily mutated to frozenset() and compared. Example -

 >>> set([1,2]) in {1,2,frozenset([1,2])} True 
+6
source share

The confusion comes because when you say β€œif set in a set”, I think python sheds his left hand into the freezon and then tests it. For example.

 >>> f = frozenset({1}) >>> f frozenset([1]) >>> x = {f, 2, 3} >>> {1} in x True 

However, there is no equivalent to frozenset for a dict, so it cannot convert a dict to an immutable object for a membership test, and therefore it fails.

I do not know how this follows this rule: is there any general method that can be overridden to provide an immutable conversion, or if this behavior is hard-coded for a particular set case in a bundle.

+7
source share

All Articles