List usage (lists)
1.list function can cut the string into List
In [2]: arr = list (' python ') in [3]: arrout[3]: [' P ', ' y ', ' t ', ' h ', ' o ', ' n ']
2. traversing the list
In [7]: For i in [' A ', ' B ', ' C ']: ...: print (i) ...: ABC
3. length, maximum value, minimum value
in []: arr = [1,3,5,-2,-8,357]in]: print (len) 6In [+]: print (max (arr)) 357In []: Print (arr)-8
4.del Delete an element from the list
in [+]: arrout[17]: [1, 3, 5,-2,-8, 357]in []: arr = [' C + + ', ' C ', ' Java ', ' HTML ']in []: Del arr[2]in []: Print (arr) [ ' C + + ', ' C ', ' HTML ']
5.list can do multiplication
In [8]: arr = [' js ', ' python ', ' css ', ' HTML ']in [9]: arr * 2out[9]: [' js ', ' python ', ' css ', ' html ', ' js ', ' python ', ' css ', ' H Tml ']in []: arr1 = [' mon ', ' tus ']in [all]: Arr + arr1out[11]: [' js ', ' python ', ' css ', ' html ', ' mon ', ' tus '
6. modifying values and slices
in [+]: arr= [' C ', ' Python ', ' css ', ' JS ', ' HTML ', ' node ']in []: arr[0]out[22]: ' C ' in [All]: arr[-1]out[23]: ' node ' in [+]: A RR[1] out[24]: ' Python ' in [+]: arr[-3]out[25]: ' JS '
7. Slice---Get a portion of the continuous data in the list
>>>arr= [' C ', ' Python ', ' css ', ' JS ', ' HTML ', ' node ']>>>print arr[1:3][' python ', ' CSS '] >>>print arr[0:1][' C ']>>>print arr[-1:-3][]>>>print arr[-3:-1][' JS ', ' HTML ']in [26]: arr= [' C ', ' Python ', ' css ', ' JS ', ' HTML ', ' node ']in [27]: print (Arr[1:3]) [ ' Python ', ' css ']in [28]: print (arr[0:1]) [' C ']in [29]: print (arr[-1:-4]) []in [30] : print (arr[-1:-3]) []in [31]: print (Arr[-3:-1]) [' JS ', ' HTML ']in [32]: print (arr[ 3:]) [' JS ', ' HTML ', ' node ']in [33]: print (arr[::]) [' C ', ' Python ', ' CSS ', ' js ', ' HTML ', ' node ']in [34]: print (Arr[:3]) [' C ', ' python ', ' CSS ']>>>print arr[-3:] [' js ', ' HTML ', ' node ']>>>print arr[3:] [' JS ', ' HTML ', ' node ']>>>print arr[:3][' C ', ' Python ', ' CSS ']>>>print arr[: ] [' C ', ' PYTHon ', ' css ', ' js ', ' HTML ', ' node '
8. Slices can be assigned values
in [+]: arrout[35]: [' C ', ' Python ', ' css ', ' JS ', ' HTML ', ' node ']in [approx]: arr[2:4]=[' pc ', ' WD '] in [PNS]: arrout[37]: [' C ', ' Python ', ' pc ', ' WD ', ' HTML ', ' node '
9. The function of slicing can be inserted into and delete elements
In [St]: arrout[39]: [' C ', ' Python ', ' pc ', ' WD ', ' HTML ', ' node ']in [+]: arr[1:1]= [' pc ', ' wd ']in [+]: arrout[41]: [' C ', ' PC ', ' wd ', ' python ', ' pc ', ' WD ', ' HTML ', ' node ']in []: arr[2:3]= [] in [+]: arrout[43]: [' C ', ' pc ', ' Python ', ' pc ', ' WD ', ' HTML ', ' node '
10.append appends the last element to the list
In []: arr= [' wd ', ' Woniu ', ' LP ']in ["]: Arr.append (' Suiji ') in []: arrout[46]: [' wd ', ' Woniu ', ' LP ', ' Suiji ']
11.count statistics The number of occurrences of an element in the list
In [a]: arr= [2,3,4,5,6,7,8,9,43,24,234,423,423,12,2,3,4,52,2]in]: Arr.count (2) out[48]: 3
12. Bubble sort
In [the]: arr= [1,2,3,123,11,123,421,124,2,3,4,5,6]in]: For I in range (len (arr)-1): ....: For L in range (len)- 1-i): .....: If arr[l]>arr[l+1]: ....: Arr[l],arr[l+1]=arr[l+1],arr[l] ....: in [5 1]: arrout[51]: [1, 2, 2, 3, 3, 4, 5, 6, 11, 123, 123, 124, 421]
13.extend extension Source list (modified original list, no return value)
in [+]: b = [7,8,9,2]in]: a = [1,2,3]in]: b = [4,5,6]in []: A.extend (b) in [The]: B.extend (a) in []: aout[58]: [ 1, 2, 3, 4, 5, 6]in [bout[59]: [4, 5, 6, 1, 2, 3, 4, 5, 6]
14.index find a value from the list and return the index position of the first occurrence
in [+]: arr= [2, ' B ', 3, ' C ', 2,4,7]in [max]: Arr.index (' B ') out[61]: 1In [+]: Arr.index (' C ') out[62]: 3
15.insert Queue Arr.insert (position, inserted content) suddenly came a beautiful woman, to jump the queue, we are embarrassed to refuse
in [+]: arr= [1,2,3,4,5,6,7]in]: Arr.insert (3, ' four ') in [all]: arrout[65]: [1, 2, 3, ' Four ', 4, 5, 6, 7]
16.pop removes the elements from the list according to the index, and returns, by default deleting the last one (without passing arguments, and append, the only way pop is the only one that modifies the source list and returns the value)
In [4In]: arr= [1,2,3,4]in]: Arr.pop () out[67]: [[]: arrout[68]: [1, 2, 3]in []: arr= [1,2,3,4]in]: Arr.pop (2 ) out[70]: 3In [[]: arrout[71]: [1, 2, 4]
17.remove Delete elements based on values, delete first match (if no match, error)
In [the]: arr= [' A ', ' B ', ' C ', ' d ']in []: Arr.remove (' a ') in []: arrout[76]: [' B ', ' C ', ' d ']# Remove if there is no match to the exception that is thrown in [ValueError]: Arr.remove (' asdf ')-----------------------------------valueerror: List.remove (x): X not in List
18.reverse List Reverse-modify lists, do not return a value
In [the]: arr= [1,2,3,4]in]: Arr.reverse () in []: arrout[80]: [4, 3, 2, 1]
19.sort methods for sorting, modifying the list itself
In [Bayi]: arr= [ -11,2,1,5]in]: Arr.sort () in []: arrout[83]: [-11, 1, 2, 5]
Some methods of Dict
This article is from the "Linux" blog, so be sure to keep this source http://liunxbk.blog.51cto.com/8683822/1750807
List of Python3