List is one of the most basic python storage methods. Different types of content can be stored through the list, such as string, number, list, dictionary, etc., which can be used as the element value of the list
The list is in square brackets [ ] Contained, different from tuples and dictionaries, the element values in the list can be obtained through the subscript index of the list.
------Let's give some list related functions as usual for reference (the table comes from python Programming (Dong Fuguo))-----
Add an element at the end of the list | list.append(x) |
Enter list L and connect its elements to the back of the list | list.extend(L) |
Add the element x where the subscript is index | list.insert(index,x) |
Delete the first occurrence of element x | list.remove(x) |
Delete the element (and return the element at that location) (default to the last element) | list.pop([index]) |
clear list | list.clear() |
Returns the subscript of the element (an exception is displayed if there is no element) | list.index(x) |
Number of counting elements | list.count(x) |
Invert the list in place | list.reverse() |
In place sorting (for Chinese, use stroke number sorting) | list.sort(key=None,reverse=False) |
Returns a shallow copy of an object | list.copy() |
Return the length of the list (irrelevant but common below) | len(list) |
Maximum | max(list) |
minimum value | min(list) |
Direct sort | sorted(list) |
Disordered order (these will be involved later (import random is required) | random.shuffle(L) |
Other related functions | map,filter,enumerate,reduce, etc |
---------------------------------------------------------------------------
1, List creation
Directly define a basic list method: a=[1,2,3,4,5] , String list: a = ["program", "rookie", "one", "Python"]
2D list: a=[[1,2,3],[4,5,6]]
There are several other ways to create a list:
1. Use the list() function to create a list
a=list() print(a)
Result: []
In addition, list can also convert tuples, dictionaries, etc. into lists
Example:
list((1,2,3,4)) #Convert tuples to lists
-[1, 2, 3, 4]
>> a_dict={'name':1,'age':2} >> list(a_dict) #Convert dictionary keys to lists only (can be used to call list contents)
-['name','age']
list can also convert strings:
list("A rookie abcd") #----['process',' sequence ',' dish ',' bird ',' one ',' only ',' a ',' B ',' C ','d']
2. Create a list using expressions, for, etc. (create a function list)
1) range,for expressions and simplest function definitions
a=list(range(10)) #Use list and range to return the list -- [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b=list(i**2 for i in range(10)) #Expressions and range s create lists - [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] c=list([] for i in range(10)) #Define something similar to an array c=[None for i in range(10)] #See for yourself
2) Use expressions in list definitions:
#--------Use the random function as an expression to create a list----- import random a=list(range(300)) b=[random.choice(a) for i in range(10)] print(b) #Generate a random list with a length of 10 between 0 and 299 #--------Define a function and use it to create------- def f(a): if (a>10): return a+10 else: return a-10 c=[f(x) for x in range(15)] print(c) #Parts less than or equal to 10 - 10, parts greater than 10 + 10 #-------------Created using lambda expressions (unnamed functions)----------------------- d=[(lambda x:x+10)(t) for t in range(5)] print(d) #Can also be used ff=lambda x: x+5 e=[ff(x) for x in range(10)] print(e)
2, List related operations
1. Value by index
a=list("arkxilingyyds") print(a[0]) #The default starts at 0 print(a[1])
2. List splicing and multiplication
x=list("program") y=list("green hand") print(x+y) #Direct splicing #----['process',' sequence ',' dish ',' bird ']----- print(x*3+y*2) #---['process',' sequence ',' process', 'sequence', 'process',' sequence ',' vegetable ',' bird ',' vegetable ',' bird ']
3. List interception
a=list("arkxilingyyds") print(a[2:]) #Intercept the tail from the element in Table 2 below print(a[:10]) #Ab initio interception print(a[5:8]) #Intercepts the specified segment print(a[-3:]) #Intercept 3 elements from the tail
4. Use the above function to add elements to the list
a=list("arkxilingyyds") a.append(list("abcdefg")) print(a) print(a.pop(10)) #Delete element with subscript 10 print(a) #Deleted list elements
Other functions are summarized in the table above and will not be repeated here
5. Sort in some way
a=['abcd','abcdefg','abc','efghi'] a.sort(key=len,reverse=False) #Sort by length print(a) #-------------Custom sort function--------- def f(x): return x**2 b=[-3,-2,-1,0,1,2,3] b.sort(key=f,reverse=False) print(b) #----[0, -1, 1, -2, 2, -3, 3]
3, List related functions enumerate,map, etc
1) enumerate purpose: construct a list with subscripts (each element is a tuple)
a=list("arkxilingyyds") b=enumerate(a) print(list(b)) #Pay attention to using list #[(0, 'a'), (1, 'r'), (2, 'k'), (3, 'x'), (4, 'i'), (5, 'l'), (6, 'i'), (7, 'n'), (8, 'g'), (9, 'y'), (10, 'y'), (11, 'd'), (12, 's')]
2) zip function
Compression list
a=[1,2,3,4] b=[5,6,7,8] c=[9,10,11,12] print(zip(a,b,c)) #[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)] print(list(map(list,zip(a,b,c)))) #This gives you a two-dimensional list [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
3) map function( map(func,L1,....) )
(reference website: map function in python rookies need to work harder - CSDN blog)
map function function: function on each element in the list and return the obtained value
The first parameter can also use list,int, etc
#-----Direct custom function------ def f(a): if (a>3): return a+10 else: return a-10 a=[1,2,3,4,5,6] b=list(map(f,a)) print(b) #------[-9, -8, -7, 14, 15, 16] #Less than 3 minus 10, more than 3 plus 10 #-----Using labda anonymous function------- ff=lambda x: x+10 #Define anonymous functions a=[5,6,7,8,9,10] b=list(map(ff,a)) #Calculate the corresponding value print(b) #----[15, 16, 17, 18, 19, 20]------- #For example, convert int to numeric c=list("2345678") print(list(map(int,c))) #---[2, 3, 4, 5, 6, 7, 8]
Relevant references:
1. (tutorial on using random library functions) Random library of python Library (random number function) _sandalphon4869 blog CSDN blog _pythonrandom Library
2. (4 ways to create lists) Four ways python creates lists besieged guest's column CSDN blog _pythoncreates lists
3. Python Programming (Third Edition) By Dong Fuguo