Lists are mutable sequences.

1. Basic operations

1.1 The methods of list objects

All of the methods of list objects as follows, excerpt from Python documentation More on Lists.

list.append(x)          # Add an item to the end of the list, i.e., a[len(a):] = [x].
list.extend(L)          # Extend the list by appending all the items in the given list, i.e., a[len(a):] = L.

list.insert(i, x)       # Insert an item at a given position. a.insert(0, x) inserts at the front of the list and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)          # Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i])           # Remove the item at the given position in the list, and return it. a.pop() removes and returns the last item in the list. 
list.clear()            # Remove all items from the list, i.e., del a.

list.index(x)           # Return the index in the list of the first item whose value is x. It is an error if there is no such item.
list.count(x)           # Return the number of times x appears in the list.

list.reverse()          # Reverse the elements of the list in place.
list.copy()             # Return a shallow copy of the list, i.e., a.

list.sort(key=None, reverse=False)  # Sort the items of the list in place.

1.2 Iteration

Use the build-in function enumerate to iterate a list with indexes:

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2022-03-16 15:42

results matching ""

    No results matching ""