Learn some Python set every day

Learn some Python set every day


A collection contains a series of elements. In Python, these elements do not need to be of the same type, and these elements have no storage order in the collection.

set assignment

Sets are represented by curly braces, which are the same as dictionaries. A set can be initialized through parentheses or constructors. If the passed parameters are repeated, they will be automatically ignored:

>>> {1,2,"hi",2.23}
{2.23, 2, 'hi', 1}
>>> set("hello")
{'l', 'h', 'e', 'o'}

Note: Since both sets and dictionaries are {}represented by, so initializing an empty set can only be done through set()operations, {}it just represents an empty dictionary

increase in collection

The addition of set elements supports two types. The addition of a single element uses the add method, and the addition of a sequence uses the update method. The role of add is similar to append in the list, and update is similar to the extend method. The update method can support passing in multiple parameters at the same time:

>>> a={1,2}
>>> a.update([3,4],[1,2,7])
>>> a
{1, 2, 3, 4, 7}
>>> a.update("hello")
>>> a
{1, 2, 3, 4, 7, 'h', 'e', 'l', 'o'}
>>> a.add("hello")
>>> a
{1, 2, 3, 4, 'hello', 7, 'h', 'e', 'l', 'o'}

Note: Adding existing elements will not affect the collection and will not throw an exception

Deletion of a collection

There are two ways to remove a single element from a collection. The difference between the two is whether an exception will be thrown when the element is not in the original collection. set.discard(x) will not, and set.remove(x) will throw a KeyError error:

>>> a={1,2,3,4}
>>> a.discard(1)
>>> a
{2, 3, 4}
>>> a.discard(1)
>>> a
{2, 3, 4}
>>> a.remove(1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: 1

The collection also supports the pop() method, but because the collection is unordered, the result returned by pop cannot be determined, and calling pop when the collection is empty will throw a KeyError error, you can call the clear method to clear the collection:

>>> a={3,"a",2.1,1}
>>> a.pop()
1
>>> a.pop()
3
>>> a.clear()
>>> a
set()
>>> a.pop()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: 'pop from an empty set'

Collection operations

  • Union: set.union(s), you can also use a|bthe calculation
  • Intersection: set.intersection(s), which can also be a&bcalculated using
  • Difference set: set.difference(s), you can also use a-bthe calculation

It should be noted that Python provides a method for finding a symmetric difference set.symmetric_difference(s), which is equivalent to finding the difference between the two sets and then finding the union. In fact, it returns the elements that appear only once in the two sets. can be a^bcalculated.

>>> a={1,2,3,4}
>>> b={3,4,5,6}
>>> a.symmetric_difference(b)
{1, 2, 5, 6}

The set.update(s) operation is equivalent to taking the union of two sets and assigning them to the original set. Several other set operations also provide their own update versions to change the value of the original set, such as intersection_update(), which can also support multiple parameter form.

containment relationship

There are generally three kinds of relationships between two sets, intersecting, containing, and disjoint. In Python, the following methods are used to judge:

  • set.isdisjoint(s): Determine whether two sets are disjoint
  • set.issuperset(s): Determine whether the set contains other sets, equivalent to a>=b
  • set.issubset(s): Determine whether the set is contained by other sets, equivalent to a<=b

If you want a true containment relationship, use the symbolic operation >and <.

immutable set

Python provides an implementation version of the set that cannot change elements, that is, cannot add or delete elements, the type is called frozenset, and the usage is as follows:

>>> a = frozenset("hello")
>>> a
frozenset({'l', 'h', 'e', 'o'})

It should be noted that frozenset can still perform set operations, but cannot use methods with update. If you want a normal collection with all the elements in frozenset, just pass it as a parameter to the collection's constructor:

>>> a = frozenset("hello")
>>> a = set(a)
>>> a.add(12)
>>> a
{'l', 12, 'h', 'e', 'o'}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325486819&siteId=291194637