A set of ordered items the variable data type "can be added to the list" can contain any data type or another list "can be any combination nested" list is a collection of data surrounded by square brackets "[]", different members with "," delimited list can access members by ordinal
Defined
>>> L = [] #空列表 >>> L = [1,2,3]>>> L = [1,2,3,[' A ', ' B ']]>>> l = list (' Linuxeye ') >> ;> l[' l ', ' I ', ' n ', ' U ', ' x ', ' e ', ' y ', ' e ']>>> l = List (range (5)) >>> l[0, 1, 2, 3, 4]>>> l = ' 1;2;3;4;5 '. Split (';') >>> l[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
The built-in function list (a_sequence) can convert a sequence to a list
Index
The positive index is the number from left to right, 0 starts, negative index number is from right to left,-1,-2 ... The range values are from left to right.
>>> languagelist = ['Python', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C ', ' C ' + ', ' Lisp ']>>> Languagelist[0] #取第一个值 ' Python ' >>> languagelist[3] #第四个值 ' php ' >>> languagelist[3:][' php ', ' Java ', ' C ', ' C + + ', ' Lisp ']>>> languagelist[:3][' Python ', ' Shell ', ' perl ']>>> languagelist[2:5][' perl ', ' PHP ', ' Java ']
>>> languagelist[-1] #倒数第一个值 ' Lisp ' >>> languagelist[-2] #倒数第二个值 ' C + + ' >>> languagelist[-5:] [' php ', ' Java ', ' C ', ' C + + ', ' Lisp ']>>> languagelist[-5:-2][' php ', ' Java ', ' C ']>>> languagelist[:-2][ ' Python ', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C '
add element
>>> languagelist[' Python ', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C ', ' C + ', ' Lisp ']>>> languagelist.append (' Go ') >>> languagelist[' Python ', ' Shell ', ' Perl ', ' PHP ', ' Java ', ' C ', ' C + ', ' Lisp ', ' Go ']>>> Languagelist.insert (3, ' Python ') #往PHP元素前插入 >>> languagelist[' python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' C + + ', ' Lisp ', ' GO ']>>> languagelist.extend ([' Ruby ', ' C # ']) >>> languagelist[' Python ', ' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C ', ' C + ', ' Lisp ', ' GO ', ' Ruby ', ' C # '
Search
>>> languagelist = [' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' C + ', ' Lisp ', ' GO ', ' Ruby ', ' C # ']&G T;>> languagelist.index (' Python ') 0>>> languagelist.index (' C ') 6>>> Languagelist.index (' C ') Traceback (most recent): File "
", line 1, in
ValueError:list.index (x): X not in List >>> ' C ' in languagelistfalse>>> languagelist.index (' Python ') #统计一个元素的出现次数2
Delete Element
>>> languagelist = [' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C ', ' C + ', ' Lisp ', ' GO ', ' Ruby ', ' C # ']&G T;>> languagelist.remove (' C ') >>> languagelist.remove (' Python ') >>> languagelist.remove (' C ' ) Traceback (most recent call last): File "
", line 1, in
ValueError:list.remove (x): X not in Li St>>> Languagelist.pop () ' C # ' >>> languagelist[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ' , ' GO ', ' Ruby ']>>> languagelist.pop ( -2) [' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' Ruby ']> >> del languagelist[-3:-1]>>> languagelist[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' Ruby '
c7/>
Remove removes only the first occurrence of a value from the list.
Pop does two things: delete the last element of the list, and then return the value of the deleted element.
modifying elements
>>> languagelist = [' Python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' Ruby ']>>> languagelist[-2] = ' C ' >>> languagelist[' python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' C ', ' Ruby ']>>> languagelist[ Languagelist.index (' C ')] = ' java ' >>> languagelist[' python ', ' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' Ruby '
Operator
>>> languagelist[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + ', ' Lisp ', ' GO ', ' Ruby ']>>> Languagelist = languagelist + [' python ', ' C ']>>> languagelist[' Shell ', ' Perl ', ' python ', ' PHP ', ' Java ', ' C + + ', ' L ISP ', ' GO ', ' Ruby ', ' Python ', ' C ']>>> languagelist + = [' java ']>>> languagelist[' Shell ', ' Perl ', ' Python ', ' PHP ', ' Java ', ' C + + ', ' Lisp ', ' GO ', ' Ruby ', ' python ', ' C ', ' java ']>>> numlist = [All-in-all] * 3>>> Numlist[1, 2, 3, 1, 2, 3, 1, 2, 3]
Sort
>>> numlist = [2,5,3,6,1,4]>>> numlist.reverse () >>> numlist[4, 1, 6, 3, 5, 2]>>> numl Ist[::-1] #和reverse效果相同 >>> numlist[2, 5, 3, 6, 1, 4]>>> numlist.sort () >>> numlist[1, 2, 3, 4, 5 , 6]
Sort acceptable parameters
CMP, compare function, accept two parameters, less than return negative, greater than return positive, equal return 0key, specify sort key reverse, specify whether to reverse order
List of comparison operations, implicit invocation of the CMP method, the comparison rule is to scan the elements for comparison, if comparable, if the next element is equal to scan, if not equal to return the result, if the two element types can not be compared, compare the ID () value of two objects. If it is equal until a list scan is complete, then a longer list is returned larger
>>> L = [(13,54), (11,59), (15,55), (12,57), (10,56)]>>> l.sort (Key=lambda x:x[1]) >>> l[(13, (]>>>), (N, (), (), (+), (+), (one, ten), (one, one), L.sort (Key=lambda x:x[1],reverse=1) #或者reverse =true>>> L [(11, 59), (12, 57), (10, 56), (15, 55), (13, 54)]
Sort and sorted function differences
Numlist = [2, 5, 3, 6, 1, 4]>>> sorted (numlist) [1, 2, 3, 4, 5, 6]>>> numlist[2, 5, 3, 6, 1, 4]>>&G T Numlist.sort () >>> numlist[1, 2, 3, 4, 5, 6]
Sort: Sorts on the original list, does not return the sorted list
Sorted: Does not change the original list, returns the sorted list