First, the collection
Collections are unordered, non-repeatable collections of data. The elements inside the collection are non-hashed (immutable types) (elements, strings, numbers), but the collection itself is not hashed (so the collection does not have a dictionary key).
Main functions:
1. Go to the weight, turn a list into a set, and automatically go heavy.
2. Relationship test, test the intersection of two sets of data, difference set, and the relationship between the set.
Create a Collection
Set = {' A ', ' B ', ' C '}
Set = Set ({' A ', ' B ', ' C '})
Print (set)
1. Go to the heavy
Set = {1,2,3,3,4,5}
Print (set)
2. Increase
Set.add (' ASD ') #增加
Set.update (' ASD ') #迭代加, an additional
3. Delete
Set.remove (' Alex ') #按元素删
Set.pop () #随机删除
Set.clear () #清空, the return value is set ()
Del set #删除字集
4. Check
Print (set)
5. Other operations
Set1 = {1,2,3,4,5}
Set2 = {4,5,6,7,8}
1) Intersection (SET1 and Set2 same element)
Print (Set1 & Set2)
Print (Set1.intersection (Set2))
2) and set (all elements of Set1 and Set2)
Print (Set1 | set2)
Print (Set1.union (Set2))
3) Difference set (there are no elements in the Set1 in the Set2)
Print (Set1-set2)
Print (Set1.difference (Set2))
4) Inverse intersection (SET1 and Set2 different elements)
Print (set1 ^ set2)
Print (Set1.symmetric_difference (Set2))
5) subset (Set1 in element Set2)
Print (Set1 < Set2)
Print (Set1.issubset (Set2))
6) Superset (SE1 all elements in Set2)
Print (Set2 > Set1)
Print (Set2.issuperset (SET1))
7) Immutable collection (Frozenset)
Set1 = {4,5}
Set2 = Frozenset (Set1)
Print (Set2,type (Set2))
Results: Frozenset ({4, 5}) <class ' Frozenset ' >
Second, in-depth copy
1. Assignment operation
L1 = [1,2,3,[' Alex ', ' Barry ']
L2 = L1
L1[0] = 111
Print (L1 is L2)
L1 and L2 point to the same memory address, so changes to L1,L2 are also changed.
2. Shallow copy
L1 = [1,2,3,[' Alex ', ' Barry ']
L2 = L1.copy ()
L1.append (' as ')
Print (L1 is L2) #结尾为False, L1 plus content, L1 is not equal to L2
L1 = [1,2,3,[' Alex ', ' Barry ']
L2 = L1.copy ()
L1[3].append (' sa ')
Print (L1[3] is l2[3]) results in the list transformation in the TRUE,L1 L2 list is also transformed.
Conclusion: Shallow copy the first layer is a different memory address, but the second layer or deeper is the same memory address.
3. Deep copy
To solve the shallow copy problem, use the Copy module.
Import Copy
L1 = [1,2,3,[' Alex ', ' Barry ']
L2 = copy.deepcopy (L1)
L1[3].append (' sa ')
Print (L1 is L2)
So the memory address of L1 and L2 is completely different.
Third, enumeration
You can add a corresponding number when the column prints the list, similar to 1.alex 2. Silver Horn 3. Goddess 4.egon 5. Taibai
Li = [' Alex ', ' Silver Horn ', ' goddess ', ' Egon ', ' Taibai ']
For Index,name in Enumerate (li,1):
Print (Index,name)
Iv. List
It is best not to delete the element in the list, and the index changes after the deletion.
Lis = [1,2,3,4,5] #要求: Delete 2,4 with a loop cannot be deleted, the index will change after deletion, can be deleted backwards, or replaced with a new list.
Method One:
For I in range (LIS) -1,-1,-1): #range (len) 0,4 first-1 starting from 4, second 1 to 1 end, third-1 backwards output
If I% 2! = 0:
Del Lis[i]
Print (LIS)
Method Two:
L = []
For I in LIS:
If Lis.index (i)% 2 ==0:
L.append (i)
Lis = L
Print (LIS)
Five, dictionary
Add to:
DIC = Dict.fromkeys ([+], ' brother Chun ')
Print (DIC) #结果: {1: ' Chun-Man ', 2: ' Chun-Elder ', 3: ' Chun-brother '}
DIC = Dict.fromkeys ([1,2,3],[])
Print (DIC) # {1: [], 2: [], 3: []}
Dic[1].append (' as ')
Print (DIC) #这几个字典共用一个内存地址, modify one change.
Delete:
DiC = {' K1 ': ' v1 ', ' K2 ': ' V2 ', ' A3 ': ' V3 '} #删除键里包含k的键值对
Method One:
s = {}
For I in DIC:
If ' K ' isn't in I:
s = {I,dic[i]}
Print (s)
Method Two:
L = []
For I in DIC:
If ' K ' in I:
L.append (i)
For I in L:
Del Dic[i]
Print (DIC)
Liu, Ganso
If the ancestor has only one element and no addition, then what type is this element, and what type is it?
TU1 = (1)
TU2 = (1,)
Print (Tu1,type (TU1), Tu2,type (TU2)) #结果: 1 <class ' int ' > (1,) <class ' tuple ' >
7th Day of Python learning