python notes -- list


1. What is a list?

A list is a data structure for an ordered set of items. After creating a list, users can access, modify, add or delete items in the list, that is, lists are mutable data types.

Second, the creation of the list

1. Use square brackets

list=[] # create an empty list

list=['hello','world',98]

2. Call the built-in function list()

list() # create an empty list

list(iterable) #Create a list containing the items in the enumerable object iterable

list(['hello','world',98])

Third, the value of the access list

1. By subscript index

s[subscript] = x # set list elements, x is any object

 

 2. Through for loop traversal

 

 3. Traverse through while loop

 4. Slicing operation

Part of a sequence(s) can be intercepted by slicing

The basic form of the slice operation is as follows: s[i:j] or s[i:j:k]

i is the start subscript of the sequence (including s[i]); j is the end subscript of the sequence (excluding s[j]); k is the step size. If i is omitted, start from index 0; if j is omitted, until the end of the sequence; if k is omitted, the step size is 1.

 

5. Determine whether the list element exists

element in listname

element not in listname

Fourth, the method of the list object

 

 Assuming the list is named list

1. Sorting of list elements

list.reverse() # reverse

list.sort() #Sort, default ascending

list.sort(reverse = True)#降序

 sorted() built-in function

2. Deletion of list elements

list.clear() #Delete all elements, equivalent to del list[:]

list.pop() # Delete according to the index, you can return the deleted value

 list.remove() deletes according to the element, only the element that appears for the first time can be deleted

3. List insert element

list.append(x) # Append the object x to the end of the list

list.insert(i,x) #Insert object x at subscript i position

list.extend(t) #The sequence t is appended to the end of the list list

4. Change list elements

Modify the list elements, find the specified position and assign

 5. List Comprehension Expressions

Using list comprehension expressions can simply and efficiently process an iterable object and generate a list of results. List comprehension expressions are as follows:

 

 

Guess you like

Origin blog.csdn.net/qq_63512287/article/details/123975834