序列类型
列表(list)
class list([iterable])
列表是一种可更改的有序集合,允许有重复的成员。在Python中 , 列表通过方括号括起、逗号分隔的一组值(元素)。一个 列表 可以包含不同类型的元素,但通常使用时各个元素类型相同。
创建列表的多种方式
使用一对方括号来表示的空列表: []
lst = []
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
[]
Process finished with exit code 0
使用方括号,其中的项以逗号分隔: [a], [a, b, c]
lst = [[1], ['a', 'b', 'c'], ['abc', 'fff']]
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
[[1], ['a', 'b', 'c'], ['abc', 'fff']]
Process finished with exit code 0
使用列表推导式: [x for x in iterable]
lst = [x for x in range(0,10)]
print(lst)
lst2 = [x for x in ['a', 12, 3, 'abcdef', [1,2,3,4,5]]]
print(lst2)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
['a', 12, 3, 'abcdef', [1, 2, 3, 4, 5]]
Process finished with exit code 0
使用list类的构造器: list() 或 list(iterable)
构造器将构造一个列表,其中的项与 iterable 中的项具有相同的的值与顺序。iterable 可以是序列、支持迭代的容器或其它可迭代对象。如果 iterable 已经是一个列表,将创建并返回其副本,类似于 iterable[:]。例如,list(‘abc’) 返回 [‘a’, ‘b’, ‘c’] 而 list( (1, 2, 3) ) 返回 [1, 2, 3]。 如果没有给出参数,构造器将创建一个空列表 []。
lst1 = list()
print(lst1)
lst2 = list(range(0,10))
print(lst2)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
[]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Process finished with exit code 0
通过索引访问列表元素
正索引
正索引从0开始。
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst[0],",",lst[1], ",",lst[2], ",", lst[3], ",", lst[4])
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
apple , banana , cherry , 1 , 2.0
Process finished with exit code 0
负索引
列表中最后一个元素的负索引为-1,倒数第二个元素的负索引为-2。
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst[-1],",",lst[-2], ",",lst[-3], ",", lst[-4], ",", lst[-5])
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
2.0 , 1 , cherry , banana , apple
Process finished with exit code 0
正索引范围(正切片)
通过指定范围的起始索引(包括)和结束索引(不包括)来指定索引范围。指定范围后,返回值将是包含指定范围的新列表。可以省略起始索引或者结束索引。
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst[1:4])
print(lst[:4])
print(lst[2:])
print(lst[:])
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['banana', 'cherry', 1]
['apple', 'banana', 'cherry', 1]
['cherry', 1, 2.0]
['apple', 'banana', 'cherry', 1, 2.0]
Process finished with exit code 0
负索引范围(负切片)
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst[-4:-1])
print(lst[-4:])
print(lst[:-1])
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['banana', 'cherry', 1]
['banana', 'cherry', 1, 2.0]
['apple', 'banana', 'cherry', 1]
Process finished with exit code 0
带步长的索引范围(切片)
lst = ["apple", "banana", "cherry", 1, 2.0, 5, 7, 8,23]
print(lst[1:6:2])
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['banana', 1, 5]
Process finished with exit code 0
更改列表元素的值
更改单个元素
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst)
lst[0] = 'orange'
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['apple', 'banana', 'cherry', 1, 2.0]
['orange', 'banana', 'cherry', 1, 2.0]
Process finished with exit code 0
更改一个列表元素
更改一个范围,可以将一个列表赋值给它,也可以将一个元组赋值给它,如果超过给定的范围,则超出的元素会插入到列表中。
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst)
lst[0:2] = ['orange', 2]
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['apple', 'banana', 'cherry', 1, 2.0]
['orange', 2, 'cherry', 1, 2.0]
Process finished with exit code 0
超过切片范围的元素会被插入到被该更元素的后面。
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst)
lst[0:2] = ['orange', 2, 1, 3]
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['apple', 'banana', 'cherry', 1, 2.0]
['orange', 2, 1, 3, 'cherry', 1, 2.0]
Process finished with exit code 0
通过元组进行修改:
lst = ["apple", "banana", "cherry", 1, 2.0]
print(lst)
lst[0:2] = ('orange', 2, 1, 3, 78)
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['apple', 'banana', 'cherry', 1, 2.0]
['orange', 2, 1, 3, 78, 'cherry', 1, 2.0]
Process finished with exit code 0
遍历列表中的元素
lst = ["apple", "banana", "cherry", 1, 2.0]
for x in lst:
print(x)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
apple
banana
cherry
1
2.0
Process finished with exit code 0
检查列表中是否存在某一元素(in)
lst = ["apple", "banana", "cherry", 1, 2.0]
if 'apple' in lst:
print("'apple' exists in 'lst' list");
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
'apple' exists in 'lst' list
Process finished with exit code 0
检查列表中是否不存在某一元素(not in)
lst = ["apple", "banana", "cherry", 1, 2.0, 5, 7, 8,23]
if 'orange' not in lst:
print("not in")
else:
print("in")
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
not in
Process finished with exit code 0
拼接列表(+)
lst = ["apple", "banana", "cherry"]
lst2 = ['1', 2, 3, '5.0']
print(lst + lst2)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['apple', 'banana', 'cherry', '1', 2, 3, '5.0']
Process finished with exit code 0
列表自身n次拼接(*)
lst = ["apple", "banana", "cherry"]
print(lst * 3)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['apple', 'banana', 'cherry', 'apple', 'banana', 'cherry', 'apple', 'banana', 'cherry']
Process finished with exit code 0
计算列表的长度(len())
lst = ["apple", "banana", "cherry"]
print(len(lst))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
3
Process finished with exit code 0
计算列表的最小项(min())
lst = ["banana", "apple", "cherry"]
lst2 = [1, 2, 3, -1]
print(min(lst))
print(min(lst2))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
apple
-1
Process finished with exit code 0
计算列表的最大项(max())
lst = ["banana", "apple", "cherry"]
lst2 = [1, 2, 3, -1]
print(max(lst))
print(max(lst2))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
cherry
3
Process finished with exit code 0
某项首次出现在列表中的索引(lst.index(x[, i[, j]]))
x首次出现的索引
lst = ["banana", "apple", "cherry", 1, 2, 3, 10, 3]
print(lst.index(3))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
5
Process finished with exit code 0
x首次出现在索引i之后的索引
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst.index('3', 1))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
2
Process finished with exit code 0
x首次出现在索引i之后并且在索引j之前的索引
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst.index('3', 2, 7))
print(lst.index('3', 3, 7))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
2
5
Process finished with exit code 0
计算某项在列表中出现的次数(count())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst.count('3'))
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
4
Process finished with exit code 0
删除列表元素(del)
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
del lst[0]
print(lst)
del lst[3:5]
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['apple', '3', '1', '2', '3', '10', '3']
['apple', '3', '1', '10', '3']
Process finished with exit code 0
按指定步长删除列表元素(del)
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
del lst[0::2]
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['apple', '1', '3', '3']
Process finished with exit code 0
列表的常用方法
向列表末尾追加元素(append())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.append('aaaaaaaaaaaaaa')
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['3', 'apple', '3', '1', '2', '3', '10', '3', 'aaaaaaaaaaaaaa']
Process finished with exit code 0
清空列表(clear())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.clear()
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
[]
Process finished with exit code 0
拷贝列表(copy())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
l = lst.copy()
print(l)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['3', 'apple', '3', '1', '2', '3', '10', '3']
Process finished with exit code 0
扩展列表(extend())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.extend(['111111', '222222222'])
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['3', 'apple', '3', '1', '2', '3', '10', '3', '111111', '222222222']
Process finished with exit code 0
在列表指定位置插入元素(insert())
如果指定的索引超过列表长度,则在列表末尾插入。
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.insert(0, '888')
print(lst)
lst.insert(100, '99999')
print(lst)
输出
`E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['888', '3', 'apple', '3', '1', '2', '3', '10', '3']
['888', '3', 'apple', '3', '1', '2', '3', '10', '3', '99999']
Process finished with exit code 0`:
提取列表中的元素(pop())
默认提取列表中的最后一个元素,也可指定索引提取指定索引位置的元素,然后从列表中删除该元素。
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.pop()
print(lst)
lst.pop(2)
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['3', 'apple', '3', '1', '2', '3', '10']
['3', 'apple', '1', '2', '3', '10']
Process finished with exit code 0
移除列表中第一个为x的元素(remove())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.remove('3')
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['3', 'apple', '3', '1', '2', '3', '10', '3']
['apple', '3', '1', '2', '3', '10', '3']
Process finished with exit code 0
将列表中的元素逆序(reverse())
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.reverse()
print(lst)
输出:
lst = ["3", "apple", "3", '1', '2', '3', '10', '3']
print(lst)
lst.reverse()
print(lst)
对列表进行排序(sort())
默认情况下,sort() 方法对列表进行升序排序。还可以通过指定函数自定义排序标准。
按照字母顺序对列表进行排序
lst = ['Porsche', 'BMW', 'Volvo']
lst.sort()
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['BMW', 'Porsche', 'Volvo']
Process finished with exit code 0
自定义排序标准
reverse可选。reverse=True 将对列表进行降序排序。默认是 reverse=False。
key 可选。指定排序标准的函数。
list.sort(reverse=True|False, key=myFunc)
降序排序:
lst = ['Porsche', 'BMW', 'Volvo']
lst.sort(reverse=True)
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['Volvo', 'Porsche', 'BMW']
Process finished with exit code 0
按照值的长度对列表进行排序:
# 返回值的长度的函数:
def myFunc(e):
return len(e)
lst = ['Porsche', 'Audi', 'BMW', 'Volvo']
lst.sort(key=myFunc)
print(lst)
输出:
E:\Python3\Exercise\venv\Scripts\python.exe E:/Python3/Exercise/venv/06.py
['BMW', 'Audi', 'Volvo', 'Porsche']
Process finished with exit code 0