Understanding set Sets in python and Their Usage

In python, a set is an unordered arrangement that can be hashed. Supports set relationship testing, does not support indexing and slicing operations, and has no specific grammatical format. It can only be created through factory functions. There will be no two identical elements in the collection. So collections are often used to de-duplicate strings or tuples or elements in lists.

Generating a collection can use the following syntax:

Generating set grammar 1: >>> l1=[1,2,3,4,5,6] >>> s1=set(l1) >>> print(s1) {1, 2, 3, 4, 5, 6} Here, a set is created using the factory function set, whose parameters can be either a list or a tuple or a string. Generating set grammar 2: >>> s2={6,7,8,9,10} >>> print(s2) {8, 9, 10, 6, 7}
Generating set grammar 3: >>> s3={i for i in range(10)} >>> print(s3) {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Method and operation of set type:

add

Adding an element to a collection if it already exists in the collection has no effect on the collection
Add an element to a set.
This has no effect if the element is already present.
>>> s1={1,2,3,4,5,6,7}
>>> s1.add(8)
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7, 8}
>>> s1.add(9)
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

clear

Clear out all elements in the collection
Remove all elements from this set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9}
>>> s1.clear()
>>> print(s1)
set()
>>> s2.clear()
>>> print(s2)
set()

copy

Shallow copies of collections (copy only elements, not memory addresses)
Return a shallow copy of a set.
>>> s1={1,2,3,4,5,6,7}
>>> print(s1,id(s1))
{1, 2, 3, 4, 5, 6, 7} 140509859430472
>>> s2=s1.copy()
>>> print(s2,id(s2))
{1, 2, 3, 4, 5, 6, 7} 140509842716712

difference

Find the difference set of two or more sets and return a new set
Return the difference of two or more sets as a new set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.difference(s2)
{1, 2, 3, 4}
>>> s2.difference(s1)
{8, 9, 10}

difference_update

Remove the intersection of two sets from the set
Remove all elements of another set from this set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.difference_update(s2)
>>> print(s1)
{1, 2, 3, 4}
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s2.difference_update(s1)
>>> print(s2)
{8, 9, 10}

discard

Remove an element from a collection. If the removed element is not in the collection, no error will be reported.
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
{1, 2, 3, 4, 5, 6, 7}
>>> s1.discard(7)
>>> print(s1)
{1, 2, 3, 4, 5, 6}
>>> s1.discard(4)
>>> print(s1)
{1, 2, 3, 5, 6}
>>> print(s1)
{1, 2, 3, 5, 6}

intersection

Find the intersection of two or more sets
Return the intersection of two sets as a new set.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.intersection(s2)
{5, 6, 7}
>>> s2.intersection(s1)
{5, 6, 7}

intersection_update

Consider the intersection of two sets as a new set
Update a set with the intersection of itself and another.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.intersection_update(s2)
>>> print(s1)
{5, 6, 7}
>>> print(s2)
{5, 6, 7, 8, 9, 10}

>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s2.intersection_update(s1)
>>> print(s2)
{5, 6, 7}
>>> print(s1)
{1, 2, 3, 4, 5, 6, 7}

isdisjoint

Two sets return True if they do not intersect
Return True if two sets have a null intersection.
>>> s1={1,2,3,4,5,6,7}
>>> s2={5,6,7,8,9,10}
>>> s1.isdisjoint(s2)
False
>>> s1={1,2,3,4}
>>> s2={6,7,8,9}
>>> s1.isdisjoint(s2)
True

issubset

If this set is a subset of the parameter set, return True
Report whether another set contains this set.
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s1.issubset(s2)
True
>>> s2.issubset(s1)
False

issuperset

If this set is a superset of parameter sets, return True
Report whether this set contains another set.
>>> s1={1,2,3,4}
>>> s2={1,2,3,4,5,6,7}
>>> s1.issuperset(s2)
False
>>> s2.issuperset(s1)
True

pop

Remove an element from a collection and report an error if the collection is empty
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
>>> s1={2,3,4,5}
>>> s1.pop()
2
>>> print(s1)
{3, 4, 5}
>>> s1.pop()
3
>>> s1.pop()
4
>>> s1.pop()
5
>>> s1.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'

remove

Remove an element from a collection and report an error if the collection is empty
Remove an element from a set; it must be a member.  
If the element is not a member, raise a KeyError.
>>> s1={1,2,3,4,5,6}
>>> s1.remove(4)
>>> print(s1)
{1, 2, 3, 5, 6}
>>> s1.remove(9)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 9

symmetric_difference

Returns the set of symmetric difference sets of two sets
Return the symmetric difference of two sets as a new set.
>>> s1={1,2,3,4}
>>> s2={6,7,8,9}
>>> s1.symmetric_difference(s2)
{1, 2, 3, 4, 6, 7, 8, 9}
>>> s3={1,2,3,4,5,6}
>>> s4={5,6,7,8,9,10}
>>> s3.symmetric_difference(s4)
{1, 2, 3, 4, 7, 8, 9, 10}

symmetric_difference_update

Make a symmetrical difference set with the parameter set and return it to itself
Update a set with the symmetric difference of itself and another.
>>> s1={1,2,3,4}
>>> s2={6,7,8,9}
>>> s2.symmetric_difference_update(s1)
>>> print(s2)
{1, 2, 3, 4, 6, 7, 8, 9}

>>> s3={1,2,3,4,5,6}
>>> s4={5,6,7,8,9,10}
>>> s3.symmetric_difference_update(s4)
>>> print(s3)
{1, 2, 3, 4, 7, 8, 9, 10}

union

Find the union of two or more sets
Return the union of sets as a new set.
>>> s1={1,2,3,4,5,6}
>>> s2={5,6,7,8,9}
>>> s1.union(s2)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

>>> s3={1,2,3,4}
>>> s4={6,7,8,9}
>>> s3.union(s4)
{1, 2, 3, 4, 6, 7, 8, 9}

update

Merge with another set and return to itself
Update a set with the union of itself and others.
>>> s3={1,2,3,4}
>>> s4={6,7,8,9}
>>> s3.update(s4)
>>> print(s3)
{1, 2, 3, 4, 6, 7, 8, 9}

Keywords: Python

Added by hotmonkey on Fri, 21 Jun 2019 02:08:34 +0300