Python removes a set from a set

According to my interpretation of the Python 2.7.2 documentation for the built-in types of 5.7 Set Types , it should be possible to remove the elements set A from set B by passing A to set.remove(elem) or set.discard(elem)

From the documentation for 2.7.2:

Note that the elem argument to the __contains__() , remove() and discard() methods can be a set.

I interpret this as meaning that I can pass set to remove(elem) or discard(elem) and all of these elements will be removed from the target set. I would use this to do something weird, like removing all vowels from a string or removing all common words from word history . Here's the test code:

 Python 2.7.2 (default, Jun 12 2011, 14:24:46) [M... Type "help", "copyright", "credits" or "license" >>> a = set(range(10)) >>> b = set(range(5,10)) >>> a set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> b set([8, 9, 5, 6, 7]) >>> a.remove(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: set([8, 9, 5, 6, 7]) >>> a.discard(b) >>> a set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> 

What I expect to return:

 >>> a set([0, 1, 2, 3, 4]) 

I know that I can accomplish this with a.difference(b) , which returns a new set; or with a set.difference_update(other) ; or with operator statements a -= b that change the set in place.

So is this a mistake in the documentation? Can set.remove(elem) not actually accept the set as an argument? Or does the documentation relate to kits? Given that difference_update is fulfilling my interpretation, I assume the latter is the case.

Is this not clear enough?

EDIT After 3 years of additional (some professional) python work and recently addressing this issue, I understand that what I'm actually trying to do can be achieved with:

 >>> c = a.difference(b) set([0,1,2,3,4]) 

because of which I initially tried to get.

+46
python set
Jan 29 2018-12-21T00:
source share
4 answers

You have already answered the question. This applies to sets of sets (in fact, sets containing freezonsets).

The paragraph you refer to begins with:

Note that the elem argument to the __contains __ (), remove (), and discard () methods can be a collection.

which means that b in a.remove(b) can be a set and then continues:

To support the search for an equivalent frozenset , the set of elements 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.

which means that if b is a set, a.remove(b) will scan a for a fennetet equivalent to b and delete it (or throw a KeyError if it does not exist).

+16
Jan 29 2018-12-21T00:
source share

You cannot have set of set in Python since set changed. Instead, you can have set from frozenset s. Alternatively, you can call __contains__() , remove() and discard() with set . See this example:

 a = set([frozenset([2])]) set([2]) in a # you get True a.remove(set([2])) # a is now empty 

So the answer to your question is that the documentation refers to set of frozenset s.

+7
Jan 29 2018-12-21T00:
source share

I am looking at the built-in help for various versions of python (for mac). Here are the results.

  • python2.5

delete (...)
Remove the item from the set; he must be a member.
If the item is not a member, raise a KeyError.

  • python2.6

delete (...)
Remove the item from the set; he must be a member. If the item is not a member, raise a KeyError.

  • python2.7

delete (...)
Remove the item from the set; he must be a member. If the item is not a member, raise a KeyError.

The documentation you refer to in its entirety actually says:

Note that the elem argument to the __contains__() , remove() and discard() methods can be a collection. To support the search for the equivalent frozenset parameter, the set of elements is temporarily mutated during the search and then restored.

This is similar to a footnote that suggests that the argument can be a set, but if it does not find the corresponding frozen set in the set, it will not be deleted. The mention of a modified set is that it can be hashed to look for the corresponding frozen set.

+2
Jan 29 '12 at 21:28
source share

I think the documentation refers to sets of (frozen) sets, yes.

+1
Jan 29 '12 at 21:15
source share



All Articles