python list list learning summary

list list

Like str int, it is a class, a basic data type

1. Representation method : The list is generally represented by [], and all classes can be put into the list, such as:

a = []
b = ['dsfn','123','Hahaha']
c = ['capture',['fdjgo','Chinese'],123456789]

When there are both strings and numbers in a list, we have to write a loop to splicing them,
but when there are only strings, we can just use the join method to splicing

2. Add, delete, modify and check the list

lst = [11,22,33,44]

a. to increase

.append() (append element) Append an element to the end of a list
.extend()     (the specified iterable) Splits a specified iterable into individual elements and appends them to the end of the list
.insert()     (specified location, specified content) Insert the specified content into the specified position of the list, for example, specify '0' to insert the top of the list
For example, lst.extend('Do whatever you want'), the last thing added to the list is 'the four elements after splitting "Do whatever you want"'

b. delete

from to[1] delete the specified element by index
.clear() clear all elements in the list
.pop() (specified position, default is last) Delete the element at the specified position in the list and return the value after deletion
.remove()     (specified content) Match the specified content with the elements in the list from left to right, and delete the same if it matches (only delete the first element that matches)

c. Modify

Elements in the list can be modified by direct assignment, for example: a[1] = 99 , or, a[1:4] = 23,'asdf',456

d. Inquiry

By slicing, specifying the index to query the element at the specified position in the list can be obtained through print() to connect the element, slice, for and while loop

3. Other methods of list list

   
.copy() Copy a list to another list, the copy here refers to a shallow copy
.count() (specified element) Counts the number of times the specified element appears in the list
.index() (specified element, start position, end position) Find the index position of the specified element in the range from left to right (seek only once)
.reverse() Reverse the elements in the list (eg [11,22,33] to [33,22,11])
.sort() (list to sort, specific order, and...) Sort the list in the specified order (this function is a little more complicated, I will open a separate article to explain)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728077&siteId=291194637