# Syntactical summary sharing of set (collection) in python3

• 2020-05-27 06:14:29
• OfStack

introduce

set is a set whose elements are singly unordered. 1 {} contains 1 element to form a collection, and set can contain multiple data types (but not lists, collections, dictionaries, tuples).

A set is a set of unordered and uncomplex elements. The fundamental work can include the test of the system and the elimination of the heavy complex element. The pooled pair also supports union
Sum),intersection(intersection),difference(difference) and sysmmetric difference(symmetry difference set).

Curly braces or the set() function can be used to create collections. Note: to create an empty collection, you must use set() instead of {}. {} is used to create an empty dictionary;

The specific syntax is summarized below.

``````
name = set(['Tom','Lucy','Ben'])
print(name)

# Output: {'Lucy', 'Juny', 'Ben', 'Tom'}``````

clear (empty all elements)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````

copy(copy set collection)

``````
name = set(['Tom','Lucy','Ben'])
new_name = name.copy()
print(new_name)

# Output: {'Tom', 'Lucy', 'Ben'}``````

difference(returns different elements from two or more collections and generates a new collection)

``````
A = set([2,3,4,5])
B = set([3,4])
C = set([2])
n = n1.difference(n2,n3)
print(n)

# Output: {5}
# return A In the set, in the B and C Elements that are not in the collection, and generate a new collection ``````

difference_update (delete elements in the A collection that exist in the B collection.)

``````
A = set([2,3,4,5])
B = set([4,5])
A.difference_update(B)
print(A)

# Output: {2, 3}``````

``````
n = set([2,3,4])
print(n)

# Output: {2, 4}``````

intersection(take the intersection and generate a new set)

``````
n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
n = n1.intersection(n2)
print(n)

# Output: {4, 5}``````

intersection_update(take the intersection, modify the original set)

``````
n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
n1.intersection_update(n2)
print(n1)

# Output: {4, 5}``````

isdisjoint(judge the intersection to return False or True)

``````
n1 = set([2,3,4,5])
n2 = set([4,5,6,7])
print(n1.isdisjoint(n2))

# Output: False``````

issubset(judgment subset)

``````
A = set([2,3])
B = set([2,3,4,5])
print(A.issubset(B))

# Output: True
#A is B A subset of the ``````

issuperset(judge the parent set)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````
0

pop(random removal of 1 element)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````
1

remove(removes specified element)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````
2

symmetric_difference(take the intersection and generate a new set)

``````
A = set([2,3,4,5])
B = set([4,5,6,7])
print(A.symmetric_difference(B))

#  Output: {2, 3, 6, 7}``````

symmetric_difference_update(take the intersection, change the original set)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````
4

union (take the union and generate a new set)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````
5

update(take the union, change the original set)

``````
name = set(['Tom','Lucy','Ben'])
name.clear()
print(name)

# Output: set()``````
6

conclusion

Related articles: