1.4 set of common methods

list_1 = [1,2,3,4,5,1,2]
# 1, to a weight (list_1 repeated removal element 2)
list_1 = set (list_1) # deduplication: {1, 2, 3, 4, 5}
print(list_1)
list_2 = set([4,5,6,7,8])
 
# 2, intersection (both in list_1 elements and list_2 4,5)
print (list_1.intersection (list_2)) # intersection: {4, 5}
 
# 3, and set (and list_2 list_1 elements of all printed, the print element is repeated only once)
print (list_1.union (list_2)) # and set: {1, 2, 3, 4, 5, 6, 7, 8}
 
# 4, difference
print (list_1.difference (list_2)) # difference set: There is no list_1 in the list_2: {1, 2, 3}
print (list_2.difference (list_1)) # difference set: There is no list_1 in the list_2: {8, 6, 7}

Guess you like

Origin www.cnblogs.com/lihouqi/p/12664230.html