The python operation list function USES code detail
- 2020-06-23 00:41:53
- OfStack
python's list is an important one, and as you learn more about it, you'll find that it's a lot to use. Recently, I often use lists when writing small projects, and sometimes I forget the methods.
So in order to review this blog, you can also come to learn 1, should be more comprehensive and detailed
List (list) :
A method of storing the same or different elements (characters) separated by commas.
One of the most important things that I think you might miss is copying lists, and I'll come back to that at the end of this article
Define a sample of three lists
lis = [1, 2, 3, 4, 5, 6]
lis = ['a', 'b', 'c', 'd']
lis = [1, 'a', '2', 'b']
All three of these are lists and they're just different types of elements but the principle is the same
The list is the storage space sorted by the following table and the following table starts at 0
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
The following table of element 4 is 3, so you can query elements with subscripts
print(lis[2:5])
The output [3, 4, 5]
This is an interception of the list, selecting an interval to output [X:Y] without Y
List with add, delete, combine, repeat, iteration, query, interception and other functions
Add append ()
lis.append(8)
lis.append('a')
The output [1, 2, 3, 4, 5, 6, 8, 'a']
append() adds elements at the end
del delete
The del method removes the list
lis = [1, 2, 3, 4, 5, 6]
del lis[3] # Pay attention to 3 It's the index of the list
print(lis) # The output [1, 2, 3, 5, 6]
remove delete
remove can also delete list elements, but it differs from del in that remove removes the first matching element (X)
lis2 = [1,2,3,2,4]
lis2.remove(2)
print(lis2) # The output [1, 3, 2, 4] The deleted is the first 1 Student: The ones that match it 2 From left to right
* repeat
The use of *, which has a similar effect to * in mathematics
lis2 = [1, 2, 3, 4]
print(lis2*3) # The output [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]
combination
The use of +, which means to combine lists with tails and heads
lis = [1, 2, 3, 4, 5, 6]
lis2 = [1, 2, 3, 4]
print(lis+lis2) # The output [1, 2, 3, 4, 5, 6, 1, 2, 3, 4]
The iteration
Lists can be iterated by the for loop, which is a feature of lists
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
0
Reverse query and intercept lists
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
1
list contains 1 of the methods
len is used to calculate the length of list
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
2
count is used to count the number of occurrences of an element in a list
lis3 = [2, 3, 5, 3, 3]
h = lis3.count(3)
print(h) # h = 3
extend extends two lists
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
4
sort sorts lists
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
5
reverse sorts the list in reverse order to sort
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
6
One way to generate lists:
[Number to generate foriinrange(range)]
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
7
Extending from above to where useful, random. choice(1,2,3) randomly selects 1 from [1,2,3] and combines it into a simple 2048 game that USES this
import random
lis5 = [random.choice([1,2,3]) for i in range(4)]
At the beginning of the article, I introduce one key point of list
Many people may ignore the problem of replication variables or lists that are simply a=b, but in python the two list should be a=list(b) to make the two list independent. This may seem like a small problem, but when you're working on a project, it's a mistake that makes you want to die (yes, I've done it so here's a tip!!).
Error :(it depends on how you use it)
lis = [1, 2, 3, 4, 5, 6]
lis[3] = 4
9
Correct:
lis6 = list(lis)
lis6.remove(4)
print(lis)
It's a good idea to knock on these two examples and see what the difference is
list also has many flexible USES and other complicated 1 point operations, but this is a simple outline of its basic usage
list is very important in python. If you find code errors or other errors in my blog when you study again, you can take a look at the explanation of official point 1 (the above code has been verified and should be ok).
conclusion
That is the end of this article's detailed code for the python operation list. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!