Understanding Set s in Python

Keywords: Python

Set, like a dictionary, is represented by {}, except that its elements are not key-value pairs, but an unordered sequence of distinct elements.
Collections are closer to the concept of sets mathematically. They can be used to determine the affiliation of data or to reduce duplicate elements in a data structure.Collections can do set operations and add and delete elements.

  • Characteristic

_Elements are out of order and not repeated
_Can do set operations

  • Basic operations

print('To create an empty collection, you must use the set() function, because if you use {} directly, you create an empty dictionary: set ():', set()).

print('Convert a string to a set(Str):', set('Tommy'))
list = ['play', 'algorithm', 'python', 2019, 5.11, 'python']

# If the elements in the collection are out of order and not repeated, python will leave only one
set1 = set(list)
print('Convert a list to a collection set(list): ', set1)

# Determine whether an element is in a collection
print("python Is it in the collection 'python' in set1: ", 'python' in set1)

# iteration
print('iteration set :')
for item in set1:
    print(item)

# Set Operations
set2 = {1, 2, 3, 4, 5, 6}
set3 = {1, 2, 7, 8}

print('Subtraction, set2 Contains but set3 Elements not included set2 - set3: ', set2 - set3)
print('Bitwise or operation, set2 and set3 All elements contained in set2 | set3: ', set2 | set3)
print('Bitwise and operation, set2 and set3 Elements included in set2 & set3: ', set2 & set3)
print('Bitwise XOR, set2 and set3 Different elements contained in set2 ^ set3: ', set2 ^ set3)
  • Output Results
To create an empty collection, you must use the set() function, because if you use {} directly, you create an empty dictionary: set():set()

Convert a string to a set: {'y','o','T','m'}

Turn a list into a collection: {'algorithm', 2019, 5.11,'python','play'}

Is Python'python'in set1:True in the collection

Iteration set:
algorithm
2019
5.11
python
play

Subtraction, element set2 - set3 contained in set2 but not in set3: {3, 4, 5, 6}
Bitwise or operation, set2 and set3 contain all elements set2 | set3: {1, 2, 3, 4, 5, 6, 7, 8}
Bitwise and operation, set2 and set3 contain elements set2 & set3:{1, 2}
Bitwise XOR operation, set2 and set3 contain elements set2 ^ set3 at different times: {3, 4, 5, 6, 7, 8}
  • Fundamental Functions
set1 = {1, 2, 3, 4, 5, 6}
print('set Length len(set): ', len(set1))
  • Output Results
Length of set len(set):6
  • Basic Methods
set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
set1.add(0)
print('Add Elements set1.add(0)', set1)

# Compute the difference set with -
print('Returns the difference between two sets set1.difference(set2)', set1.difference(set2))

print('Returns the difference between two sets set2.difference(set1)', set2.difference(set1))


set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
# Remove elements that exist in both sets
set1.difference_update(set2)
print('remove set1 in set1 and set2 Elements that all exist set1.difference_update(set2)', set1)


set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
set2.difference_update(set1)
print('remove set2 in set1 and set2 Elements that all exist set2.difference_update(set1)', set2)


# Removing Elements
set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
# Remove the specified element
set1.discard(10)
print("Error-free when removed elements do not exist set1.discard(10): ", set1)
set1.remove(6)
print("Error when removed element does not exist set1.remove(6): ", set1)
# Random Removal of Elements
set1.pop()
print('After randomly removing elements set1 set1.pop(): ', set1)

set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
set3 = {5, 6, 10}
set4 = {5, 6, 7, 8, 9}
print('Returns the intersection of multiple sets, set1 Unchanged set1.intersection(set2,set3,set4): ', set1.intersection(set2,set3,set4), set1)

set1.intersection_update(set2,set3,set4)
print('set1 Become set1 Intersection with other collections set1.intersection_update(set2,set3,set4): ', set1)


set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6}

print('Determines whether two collections contain the same elements, if they contain returns False set2.isdisjoint(set1): ', set2.isdisjoint(set1))

# Subset Judgment
print('judge set1 Is it set2 A subset of set1.issubset(set2): ', set1.issubset(set2))
print('judge set2 Is it set1 A subset of set2.issubset(set1): ', set2.issubset(set1))

# Parent Set Judgment
print('judge set1 Is it set2 The parent set of set1.issuperset(set2): ', set1.issuperset(set2))
print('judge set2 Is it set1 The parent set of set2.issuperset(set1): ', set2.issuperset(set1))


set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 9}
print("xor set1.symmetric_difference(set2): ", set1.symmetric_difference(set2))
set1.symmetric_difference_update(set2)
print("Remove the same part and add different parts to it after XOR set1 in set1.symmetric_difference_update(set2): ", set1)

set1 = {1, 2, 3, 4, 5, 6}
set2 = {4, 5, 6, 7, 8, 9}
set3 = {5, 6, 10}
print('Compute Set of Sets set1.union(set2, set3): ', set1.union(set2, set3))

set1 = {1, 2, 3, 4, 5, 6}
set2 = {5, 6, 10}
set1.update(set2)
print('take set2 Elements added to set1 in set1.update(set2): ', set1)

print('set.clear() and set.copy()Usage Same List')
  • Output Results
Add element set1.add(0) {0, 1, 2, 3, 4, 5, 6}

Returns the difference set 1.difference (set2) of two sets {0, 1, 2, 3}
Returns the difference set 2.difference (set1) between two sets {8, 9, 7}

Remove the element set1.difference_update(set2) {1, 2, 3} that exists in both Set1 and set2 in Set1
 Remove the element set2.difference_update(set1) {7, 8, 9} that exists in both Set1 and set2 in set2

set1.discard(10): {1, 2, 3, 4, 5, 6} when removed elements do not exist
 Error set1.remove(6) when removed element does not exist: {1, 2, 3, 4, 5}
set1 set1.pop() after randomly removing elements: {2, 3, 4, 5}

Returns the intersection of multiple sets, Set1 unchanged set1.intersection(set2,set3,set4): {5, 6} {1, 2, 3, 4, 5, 6}
Set1 becomes the intersection of Set1 and other collections set1.intersection_update(set2,set3,set4): {5, 6}

Determines whether two collections contain the same element, if they contain False set2.isdisjoint(set1):False

Determine if Set1 is a subset of set2, set1.issubset(set2):False
 Determine if set2 is a subset of set1, set2.issubset(set1):True

Determine if Set1 is the parent set of set2, set1.issuperset(set2):True
 Determine if set2 is the parent set of set1, set2.issuperset(set1):False

XOR operation set1.symmetric_difference(set2): {1, 2, 3, 9}
Remove the same part after XOR operation, add different parts to set1.symmetric_difference_update(set2): {1, 2, 3, 9}

set1.union(set2, set3) of a calculated set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Add elements from set2 to set1.update(set2): {1, 2, 3, 4, 5, 6, 10}

set.clear() and set.copy() are used in the same list
18 original articles published. 2. 554 visits
Private letter follow

Posted by blogfisher on Sun, 01 Mar 2020 18:10:19 -0800