list list
Like str int, it is a class, a basic data type1. 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 |
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
可以通过直接赋值的方式给列表中的元素进行修改,例如:a[1] = 99 ,或者,a[1:4] = 23,'asdf',456d.查询
通过切片,指定索引来查询列表中指定位置的元素可以通过print()接上元素,切片,for和while循环获取
3.列表list的其它方法
.copy() | 将一个列表复制到另一个列表,这里的复制指的是浅复制 |
.count() (指定的元素) | 计算指定的元素在列表中出现的次数 |
.index() (指定的元素,起始位置,结束位置) | 从左到右寻找指定元素在范围内的索引位置(只寻找一次) |
.reverse() | 将列表中的元素反转 (比如[11,22,33]反转为[33,22,11]) |
.sort() (要排序的列表,特定的顺序,以及...) | 将列表按照指定的顺序排序(这个函数稍复杂我会开辟一个单独的文章进行说明) |