python list query method

  1. List index, such as a=[1,2,3,4,5], use a[2] to extract the third element
  2. List slicing, a[:3], a[-2:], a[0:2] and other slices to extract multiple elements;
  3. List member operator, such as 2 in a, the result is True;
  4. List methods: append() adds an element to the end of the list; extend() adds all the elements of another list to the end of the list, insert() inserts an element at the specified position, and remove() deletes the first matching element in the list. , pop() deletes the element at the specified position in the list and returns the value of the element; index() returns the subscript of the first matching element in the list; count() returns the number of occurrences of the specified element in the list; sort() The list is sorted; reverse() reverses the elements in the list;
  5. List comprehension: List comprehension is an advanced query method that generates a new list through one line of code; the syntax of list comprehension is [expression for item in iterable], where expression represents the element to be generated and item represents the element that can be generated. Iterate each element in the object. Iterable represents an iterable object. If we want to generate a list consisting of squares from 1 to 10, we can use the list comprehension: [x**2 for x in range (1,11)]

Guess you like

Origin blog.csdn.net/Darin2017/article/details/132081630