Set characteristics: unordered, non-repeating, can be nested
Create set
Set_example = Set ("123", "213", "234", "432")
S1 = [11,22,33,44,11,22,33]
S2 = set (S1)
Print (s2)
The list () is equivalent to the execution of the constructor method __int__, which executes a for loop, loop (11,22,33,44,11,22,33) Set difference
S1 = {11, 33, 44, 66}
S2 = {22, 33, 44, 55, 88}
S1 exists in the S2
S3 = s1.difference (S2)
Print (S3)
Variance results updated to S1
S1.difference_update (S2)
Print (S1)
S1.symmetric_difference_update (S2)
Print (S1)
Removes the specified element, there is a removal, there is no action, discard most useful
S1.discard (11)
Print (S1)
Removes the specified element without an error
S1.remove (11111)
Random removal
S1 = {11, 33, 44, 66}
S3 = S1.pop ()
Print (S1)
Print (S3)
Intersection
S3 = s1.intersection (S2)
Print (S3)
Intersection update to S1
S1.intersection_update (S2)
Print (S1)
Whether the parent-child sequence
S1.issubset (S2)
S1.issuperset (S2)
and set
S3 = s1.union (S2)
Print (S3)
Cyclic updates (strings, lists, tuples) are objects that can be used for loops
S1.update ([11,22,33,44,55,66,77,88,99,00])
Print (S1)
Set Exercises
Old_dict = {
"#1": 8,
"#2": 4,
"#4": 2,
}
New_dict = {
"#1": 4,
"#2": 4,
"#3": 2,
}
#应该删除哪几个槽位
Old_set = Set (Old_dict.keys ())
New_set = Set (New_dict.keys ())
Old_set.difference ()
#需要删除的, there is no new in old.
Remove_set = Old_set.difference (New_set)
Print ("Slot to delete:%s"% remove_set)
#需要增加的, there's old in new.
Add_set = New_set.difference (Old_set)
Print ("Additional Slots required:%s"% add_set)
#需要更新的, the intersection of old and new
Update_set = Old_set.intersection (New_set)
Print ("Slots required for update:%s"% update_set)
Day3 set sets, functions, and adorners