Article From:

【Set characteristics]
   1、Natural weight, cycle

   2 Relational testing – intersection, difference set, union, (reverse difference set, symmetric difference set)


list = [1,2,3,4,5,3,6]
list_2 =[2,3,5,7,8]
list=set(list)    #Go heavy, turn a set
list_2 = set(list_2)
print(list.intersection(list_2))        #Intersecting, taking out repeated data
print(list.union(list_2))                 # Integration, weight removal and unified display
print(list.difference(list_2))          #Difference set, take out list_2 in list
list_3 = set([1,3,6])
print(list_3.issubset(list))             #The values of the subset list_3 are all in the list
print(list.issuperset(list_3))          #Parent set
print(list.symmetric_difference(list_2))        # Symmetric difference sets LSIT and list_3 are not mutually exclusive

Another method of representation of a set relation:

print(list & list_2) #intersection
print(list | list_2) # Union
print(list -list_2) #Difference set
print(list ^ list_2) # Symmetric difference set

【Set operation]

list.add(777)      #Add one at a time
list.update([888,999])     #Add more than one at the same time

list.remove(999)      #Delete does not exist, will be wrong
list.discard(888)       # Deleting a non – existent will not make a mistake

The collection is also unordered

nums = {1,2,3,4,5}

for n in nums:



Link of this Article: Python base (four) set

Leave a Reply

Your email address will not be published. Required fields are marked *