Deconstruction
In [8]: LST = [1,2]in [9]: lstout[9]: [1, 2]in [ten]: First,second = LST #解构In [all]: Print (First,second) 1 2
Assign the elements of a linear structure to a variable in the order of elements
The change of deconstruction
in [+]: LST = list (range) in [[]: lstout[22]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]in []: Head,*mid,tail = lstin []: Hea DOUT[24]: 0In [tailout[25]: 9In []: midout[26]: [1, 2, 3, 4, 5, 6, 7, 8]in []: Head,*_,tail = lstin []: HeadOut []: 0In []: tailout[29]: 9
Python is a convention that uses a single underscore _ to discard the variable
Packaging
in [[]: T = 1,2in [+]: tout[13]: (1, 2) in [+]: type (t) out[14]: Tuplein []: T1 = (+) in [+]: t2 = 1,2in [+]: t1out[ []: (1, 2) in []: t2out[18]: (1, 2)
The package must be ganso.
Collection--No duplicate elements
Definition and initialization
in [+]: s = set () in [+]: sout[32]: Set () in [[]: s = {1,2,3}in]: sout[34]: {1, 2, 3}in [+]: s = Set (range (5)) in [36 ]: sout[36]: {0, 1, 2, 3, 4}
Operation of the collection
Increase
In [PNS]: sout[37]: {0, 1, 2, 3, 4}in [MAX]: S.add (9) #增加单个元素In [[]: sout[39]: {0, 1, 2, 3, 4, 9}in [+]: S.add (9) #增加已经存 In the element, do nothing in [the]: sout[41]: {0, 1, 2, 3, 4, 9}in []: S.update (Range (10,15)) #增加多个元素In []: sout[43]: {0, 1, 2, 3, 4, 9 , 10, 11, 12, 13, 14}
Delete
in [44]: sout[44]: {0, 1, 2, 3, 4, 9, 10, 11, 12, 13, 14}in [45]: s.remove (1) In [46]: sOut[46] : {0, 2, 3, 4, 9, 10, 11, 12, 13, 14}in [47]: S.pop () out[47]: 0in [48]: sout[48]: {2, 3, 4, 9, 10, 11, 12, 13, 14}in [49]: s.pop () Out[49]: 2in [50]: s.pop () Out[50]: 3In [ 51]: s.pop () Out[51]: 4in [52]: s.pop () Out[52]: 9in [53]: s.pop () Out[53]: 10in [54]: s.pop () Out[54]: 11in [55]: s.discard () In [56]: sOut[56]:  {12, 14}
Remove removes the given element, the element does not exist throw Keyerror
Discard Delete the given element, the element does not exist, nothing is done
Pop randomly deletes an element and returns the set as empty, throwing keyerror
Clear Empty Collection
A collection cannot modify a single element, cannot be found by an index, a collection does not have access to a single element, the collection element has no order, and the element must be hashed
Set operation
Intersection, set, difference set, differential set
In [the]: S1 = {1,2,3}in []: s2 = {2,3,4}in []: s1.intersection (S2) out[59]: {2, 3}in []: S1 & s2out[60]: {2, 3}in [A]: in [s1.union]: S2 out[61]: {1, 2, 3, 4}in [+]: S1 | S2OUT[62]: {1, 2, 3, 4}in [+]: in []: s1.difference (S2) out[63]: {1}in [+]: s1-s2out[64]: {1}in [+]: in []: S1.sym Metric_difference (S2) out[65]: {1, 4}in [+]: S1 ^ s2out[66]: {1, 4}
The judgment of the collection
In []: S1 = {1,2,3,4}in []: s2 = {2,3}in []: S2.issubset (S1) out[69]: Truein []: S1.issuperset (S2) out[70]: Truein [ []: in [+]: in []: S1.isdisjoint (S2) # Determine if two sets have intersection, if there is a intersection return False, no intersection returns TRUEOUT[71]: False
Dictionary--is a key-value structure
Definition and initialization
In [the]: D = {}in []: D out[73]: {}in []: D = dict{}in []: D = dict () in []: dout[76]: {}in [all]: D = {' A ': 1, ' B ': 2} In [the]: dout[78]: {' A ': 1, ' B ': 2}in [+]: D = dict ([' A ', 1], (' B ', 2)]) in []: dout[80]: {' A ': 1, ' B ': 2}in [Bayi]: D = Dict.fromkeys (Range (5), ' AB ') in []: dout[82]: {0: ' ab ', 1: ' Ab ', 2: ' AB ', 3: ' AB ', 4: ' AB '}
Add, modify
In [the]: dout[83]: {0: ' ab ', 1: ' Ab ', 2: ' AB ', 3: ' AB ', 4: ' AB '}in [+]: in [+]: d[' a '] = 5In []: dout[85]: {0: ' ab ', 1 : ' AB ', 2: ' AB ', 3: ' AB ', 4: ' Ab ', ' a ': 5}in [[D.update]: (' C ', 6)]) in []: dout[87]: {0: ' ab ', 1: ' Ab ', 2: ' AB ', 3: ' AB ', 4: ' Ab ', ' a ': 5, ' B ': 5, ' C ': 6}in [in]: D.update ({' d ': 2, ' R ': 6}) in []: dout[89]: {0: ' ab ', 1: ' Ab ', 2: ' AB ', 3: ' AB ', 4: ' Ab ', ' a ': 5, ' d ': 2, ' B ': 5, ' C ': 6, ' R ': 6}
Delete
In [the]: D.pop (0) out[91]: ' AB ' in [the]: dout[92]: {1: ' AB ', 2: ' AB ', 3: ' AB ', 4: ' Ab ', ' a ': 5, ' d ': 2, ' B ': 5, ' C ': 6, ' R ': 6}in []: D.popitem () #随机删除Out [[]: (1, ' AB ')
The traversal of a dictionary
in [100]: dout[100]: {' B ': 5, ' C ': 6, ' R ': 6}in [101]: for x in d: ...: print (x) ...: bcrin [102]: d.keys () Out[102]: dict_keys ([' B ', ' C ', ' R ']) In [103]: d.values () out[103]: dict_values ([5, 6, 6]) In [ 104]: d.items () out[104]: dict_items ([' B ', 5), (' C ', 6), (' R ', 6)]) In [105]: for v in d.values (): ...: print (v) ...: 566In [106]: for X in d.items (): ...: print (x) ...: (' B ', 5) (' C ', 6) (' R ', 6) in [107]: for k,v in d.items (): ...: print (k,v) ...: b 5c 6r 6
The key of the dictionary cannot be duplicated, it needs to be hashed
This article from "Thick tak" blog, declined reprint!
Python built-in data structure 3