1. Supplementary data types 1, 1 tuples
When there is only one element in the tuple and there is no comma, the data type is the same as the element inside the parentheses.
TU1 = (' Laonanhai ') tu2 = (' Laonanhai ') print (TU1, type (TU1)) print (TU2, type (TU2))
TU1 = (1) tu2 = (1,) print (TU1, type (TU1)) print (TU2, type (TU2))
TU1 = ([1, 2, 3]) TU2 = ([1, 2, 3],) print (TU1, type (TU1)) print (TU2, type (TU2))
1, 2 list
When looping through a list, it's best not to change the size of the list, which can affect your final result.
Li = [111, 222, 333, 444, 555,], all elements with an odd index are all deleted.
Method One:
L1 = [111, 222, 333, 444, 555,]del l1[1::2]print (L1)
Method Two (Error display):
L1 = [111, 222, 333, 444, 555,]for index in range (len (L1)): print (' delete previous index:%s '% index) print (' Delete before l1:%s '% l 1) If index% 2 = = 1: del L1[index] print (' Delete after index:%s '% index) print (' Delete after l1:%s '% L1) print (L1)
Method Three (inverted delete):
L1 = [111, 222, 333, 444, 555,]for index in range (len (L1)-1,-1,-1): If index% 2 = = 1: del l1[index]print (L1)
Method Four (self-made):
L1 = [111, 222, 333, 444, 555, 666, 777, 888, 999,]i = Len (l1) count = 1s = int (len (L1)/2) for J in Range (s): del L1[co UNT] count + = 1print (L1)
1, 3 dictionary 1, 3, 1 Dict.fromkeys (A, B) A is an iterative object, B is arbitrary. Create a dictionary with the smallest element of iteration A as the key and B as the value.
DIC = Dict.fromkeys (' abc ', 666) print (DIC)
DIC = Dict.fromkeys ([One, one, one], 666) print (DIC)
DIC = Dict.fromkeys ([1, 2, 3], []) dic[1].append (666) print (DIC)
1, 3, 2 in the circulation dictionary, it is best not to change the size of the dictionary, will affect the results or error.
Error Example:
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 ', ' name ': ' Alex '}for i in dic: if ' K ' in I: del dic[i]print (DIC)
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' V3 ', ' name ': ' alex '}l1 = []for key in dic: if ' K ' in key: l1.append (key)
print (L1) for key in L1: del dic[key]print (DIC)
1, 4 conversion of data types
str------> LIST:
Split
List----->STR:
Join
Tuple------> List:
TU1 = (1, 2, 3) L1 = list (tu1) print (L1)
List------> Tuple:
TU1 = (1, 2, 3) L1 = List (TU1) tu2 = tuple (L1) print (TU2)
DIC------> List:
Only key is listed in the list (DIC).
DiC = {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' K3 ': ' v3 ',}l1 = List (dic) print (L1) print (List (Dic.keys ())) Print (List (dic.values ())) print (List (Dic.items ()))
0, ', [], {}, ()------> bool are false.
Print (bool (0)) print (bool (")) print (bool ())) print (bool ([])) print (bool ({}))
Print (bool ([0, 0, 0]))
2. Set Set
Set1 = {1, 2, 3, ' abc ', (1, 2, 3), True,}print (Set1)
2, 1 go to heavy set to go heavy.
Set2 = {One, one, one, one, 22}print (Set2)
The de-weight of the list.
L1 = [One, one, one, one, one, one, all, 44]L2 = List (set (L1)) L2.sort () print (L2)
2, 2 __.add (' a ') A for added content, randomly added.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.add (' Taibai ') print (SET1)
__.update (' A ') A is an iterative content that is added, splitting A into the smallest unit and then iterating over the addition.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.update (' abc ') print (SET1)
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.update ([111, 2222, 333]) print (SET1)
2, 3 Delete
__.remove (' A ') is deleted by element. A is the content that needs to be deleted.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.remove (' Ritian ') print (SET1)
__.pop () is randomly deleted and has a return value.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}print (Set1.pop ()) print (SET1)
__.clear () empties the collection. An empty collection is set ().
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}set1.clear () print (SET1)
Del __ Deletes the collection.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}del Set1print (Set1)
2, 4 check
For loop.
Set1 = {' Alex ', ' Wusir ', ' Ritian ', ' Egon ', ' Barry '}for I in Set1: print (i)
2, 5 intersection
Intersection: __ & __ or __.intersection (__)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1 & Set2print (SET3)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1.intersection (set2) print (SET3)
2, 6 and set
The Set: __ | __ or __.union (__)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1 | Set2print (SET3)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1.union (set2) print (SET3)
2, 7 difference set
Difference set: __-__ or __.difference (__)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1-set2print (set3) # Set1 Unique
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1.difference (set2) # Set1 unique print (SET3)
2, 8 inverse intersection
Anti-intersection: __ ^ __ or __.symmetric_difference (__)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1 ^ set2print (set3)
Set1 = {1, 2, 3, 4, 5}set2 = {4, 5, 6, 7, 8}set3 = Set1.symmetric_difference (set2) print (SET3)
2, 9 subsets
Subset: __ < __ or __.issubset (__)
Set1 = {1, 2, 3}set2 = {1, 2, 3, 4, 5, 6}print (Set1 < Set2) # True Set1 is a subset of Set2
Set1 = {1, 2, 3}set2 = {1, 2, 3, 4, 5, 6}print (Set1.issubset (Set2)) # True Set1 is a subset of Set2
2, 10 hyper-set
Set1 = {1, 2, 3}set2 = {1, 2, 3, 4, 5, 6}print (Set2 > Set1) # Set2 is a superset of Set1
Set1 = {1, 2, 3}set2 = {1, 2, 3, 4, 5, 6}print (Set2.issuperset (Set1)) # Set2 is a superset of Set1
Frozenset () Frozenset is a frozen collection that is immutable and has a hash value, and the advantage is that it can be either a key to the dictionary or an element of another collection. The disadvantage is that once created, it cannot be changed without the Add,remove method.
Set1 = Frozenset ({1, 2, 3, ' Alex '}) print (Set1)
3, the depth copy for the assignment operation, the point is the same memory address, a change has changed.
L1 = [1, 2, 3]l2 = L1l3 = L2l3.append (666) print (L1, L2, L3)
3, 1 Shallow copy
Copy. () Shallow copy
L1 = [One, one, 33]l2 = L1.copy () l1.append (666) print (L1, id (L1)) print (L2, ID (L2))
Shallow copy follows the change when the list of inner layers adds elements. The list of the inner layers is also the same address.
For shallow copy, the first layer creates a new memory address, starting at the second level and pointing to the same memory address, so consistency is maintained for the second layer and the deeper layers.
L1 = [One, one, [' Barry ', []], [one, 22]]l2 = L1.copy () l1[2].append (' Alex ') print (L1, id (L1)) print (L2, ID (L2)) print (L1, ID (l1[-1])) print (L2, id (l2[-1]))
3, 2 deep copy
Import Copy
Import COPYL1 = [One, one, 33]l2 = copy.deepcopy (L1) l1.append (666) print (L1, id (L1)) print (L2, ID (L2))
Import COPYL1 = [one, one, [' barry ']]l2 = copy.deepcopy (L1) l1[2].append (' Alex ') print (L1, id (l1[-1])) print (L2, id (l2[-1]))
Deep copy is completely independent.
L1 = [1, 2, 3]l2 = [1, 2, 3]l1.append (666) print (L1, id (L1)) print (L2, ID (L2))
For slices, this is a shallow copy.
L1 = [1, 2, 3, 4, 5, 6, [one, 22]]l2 = L1[:]l1.append (666) print (L1, L2)
L1 = [1, 2, 3, 4, 5, 6, [one, 22]]l2 = L1[:]l1[-1].append (666) print (L1, L2)
Python full stack __ data type supplement, set set, depth copy