1.set set 1.set method resolution 1. Define a set
" caesary " = Set (S1)print= [1,2,3,4= set (L1)print= (1,1,1,2,3= set (t1)print(SET3)
Execution results are as follows
{'y'e's'R 'c'a'} {1, 2, 3, 4 } {1, 2, 3}
Summary: The set set has a redo function, similar to the awk array, and is unordered, similar to a dictionary.
2.add ()
Print (Set1) set1.add ("x") Print (Set1)
Execution results are as follows
{'e','a','s','C','R','y'}{'e','a','s','C','R','x','y'}
Summary: The Add () method adds an element to the collection.
3.clear ()
Print (Set1) set1.clear () Print (Set1)
Execution results are as follows
{'s'a', 'r'C ' e'y'}set ()
Summary: The clear () method can empty a collection.
4.copy ()
Print= set1.copy ()print(SET1,SET4)
Execution results are as follows
{'s','a','y','C','R','e'}{'s','a','y','C','R','e'} {'C','R','e','s','a','y'}
Summary: Copy () can briefly copy a collection.
5.difference (), Difference_update ()
Print= set2.difference (set3)print(set5,set2) set3.difference_update (Set2) Print(SET2,SET3)
Execution results are as follows
{1, 2, 3, 4} {1, 2, 3, 5} {4} {1, 2, 3, 4} {1, 2, 3, 4} {5}
Summary: The difference () method is to remove the elements that are common to the parameter collection from this collection and assign them to a collection that does not change the collection and the Parameter collection. The Difference_update () method is the element that is removed from this collection and is common to the parameter collection, acting on this collection.
6.discard (), remove ()
Common:
Print (Set3) Set3.discard (2) Print (Set3) set3.remove (1) Print (SET3)
Execution results are as follows
{1, 2, 3, 5} {1, 3, 5}{3, 5}
Different points:
T1 = (1,1,1,2,3,5= set (t1)print(set3) Set3.discard (6)print (Set3) set.remove (6) Print (SET3)
Execution results are as follows
traceback (most recent call last): File /users/liukai/pycharmprojects/s13/day3/blog.py , line, in <module> Set.remove ( 6) typeerror:descriptor remove " requires a '
Summary: The discard (), remove () method is to delete an element, the difference is that discard () is deleted, no pass, and remove () method is deleted, no exception, it is strongly recommended to use the discard ()
7.intersection (), Intersection_update ()
Print= set2.intersection (set3)print(SET7)
Execution results are as follows
{1, 2, 3, 4} {1, 2, 3, 5} {1, 2, 3}
Summary: the intersection () method returns a collection of intersections for this collection and a parameter collection.
Print (Set2,set3) set2.intersection_update (SET3) Print (Set2)
Execution results are as follows
{0, 1, 2, 3, 4} {1, 2, 3, 5} {1, 2, 3}
Summary: The Intersection_update () method returns a collection of intersections for this collection and a collection of parameters for use in this collection.
8.update ()
Print (Set2) set2.update ({0,0,0,9}) Print (Set2)
Execution results are as follows
{1, 2, 31, 2, 3, 9}
Summary: The update () method updates a collection directly into this set to merge the weight.
2.set Application Example Analysis
There are two dictionaries that need to update the new dictionary to the old dictionary:
Old_dict = { "#1": 8, "#2": 4, "#4": 2,}new_dict= { "#1": 4, "#2": 4, "#3": 2,}
Problem Solving Ideas:
1. You need to ask for an entry to be deleted from the old dictionary.
2. New dictionaries need to be added to the old dictionary entries.
3. Require a new dictionary to be updated to the old dictionary entries.
Problem Solving methods:
## #set (), convert the old dictionary key into a collectionOld_key_set =Set (Old_dict.keys ())## #set (), convert the key of the new dictionary into a collectionNew_key_set =Set (New_dict.keys ())## #定义一个需要删除的集合, set of difference () to find the key in the old dictionary instead of the set of keys in the new dictionaryRemove_set =old_key_set.difference (New_key_set)## #删除老字典的条目delold_dict["". Join (List (remove_set))]## #定义一个需要新增的集合Update_set =new_key_set.difference (Old_key_set)## #新增新字典的条目old_dict["". Join (List (update_set))] = new_dict["". Join (List (update_set))]## #查找需要更新的key, the way I take it is to traverse two dictionaries forIinchold_dict: forJinchnew_dict:ifi = = J andOld_dict[i]! =New_dict[j]: old_dict[i]=New_dict[j]Print(old_dict)
Results:
{'#1'#2'#3': 2}
Python Weekly third week