第3章 列表简介
3.1 列表是什么
列表由一系列按特定顺序排列的元素组成。
用方括号([ ])表示列表,并用逗号分隔其中的元素。
# bicucles.py
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
# Python将打印列表的内部表示,包括方括号
print(bicycles)
3.1.1 访问列表元素
列表是有序集合,因此要访问列表的任意元素,只需将该元素的位置(索引)告诉Python即可。要访问列表元素,可指出列表的名称、再指出元素的索引,并将后者放在方括号内。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
print(bicycles[0].title())
3.1.2 索引从0而不是1开始
在Python中,第一个列表元素的索引为0,而不是1。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])
Python为访问最后一个列表元素提供了一种特殊语法。通过将索引指定为-1,可让Python返回最后一个列表元素。这种约定也适用于其他负数索引。例如,索引-2返回倒数第二个列表元素,索引-3返回倒数第三个列表元素,依次类推。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])
3.1.3 使用列表中的各个值
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."
print(message)
3.2 修改、添加和删除元素
3.2.1 修改列表元素
要修改列表元素,可指定列表名和要修改的元素的索引,再指定该元素的新值。
# motorcycles.py
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
3.2.2 在列表中添加元素
1、在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
# 方法append()将元素添加到列表末尾,而不影响列表中的其他所有元素
motorcycles.append('ducati')
print(motorcycles)
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
2、在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motocycles)
# 方法insert()可在列表的任何位置添加新元素,需要指定新元素的索引和值
motocycles.insert(0, 'ducati')
print(motocycles)
3、从列表中删除元素
(1)使用del语句删除元素:如果知道要删除的元素在列表中的位置,可使用del语句
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
(2)使用方法pop()删除元素:方法pop()删除列表末尾的元素,并让你能够接着使用它
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motocycle = motorcycles.pop()
print(motorcycles)
print(popped_motocycle)
(3)弹出列表中任何位置处的元素:可以使用pop()来删除列表中任意位置的元素,只需在圆括号中指定要删除元素的索引即可
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motocycles)
first_owned = motocycles.pop(0)
print(f"The first motorcycles I owned was a {first_owned.title()}.")
(4)根据值删除元素:如果只知道要删除的元素的值,可使用方法remove(),使用remove()从列表中删除元素时,也可接着使用它的值。方法remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来确保将每个值都删除。
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
3.3 组织列表
3.3.1 使用方法sort()对列表永久排序
# cars.py
cars = ['bmw', 'audi', 'toyota', 'subaru']
# 方法sort()永久性地修改列表元素的排列顺序,还可按与字母顺序相反的顺序排列列表元素,只需向sort()方法传递参数reverse=True即可
cars.sort()
print(cars)
cars.sort(reverse=True)
print(cars)
3.3.2 使用函数sorted()对列表临时排序
要保留列表元素原来的排列顺序,同时以特定的顺序呈现它们,可使用函数sorted()。函数sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。如果要按与字母顺序相反的顺序显示列表,也可向函数sorted()传递参数reverse=True。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)
print("\nHere is the reverse sorted list:")
print(sorted(cars, reverse=True))
3.3.3 倒着打印列表
要反转列表元素的排列顺序,可使用方法reverse()。方法reverse()永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,只需对列表再次调用reverse()即可。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
cars.reverse()
print(cars)
3.3.4 确定列表的长度
使用函数len()可快速获悉列表的长度。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
3.4 使用列表时避免索引错误
索引错误意味着Python在指定索引处找不到元素。
当列表为空时,用索引-1访问最后一个列表元素会导致索引错误。
3.5 小结
在本章中,你学习了:列表是什么以及如何使用其中的元素;如何定义列表以及如何增删元素;如何对列表进行永久性排序,以及如何为展示列表而进行临时排序;如何确定列表的长度,以及在使用列表时如何避免索引错误。
在第4章,你将学习如何以更高效的方式处理列表元素。通过使用为数不多的几行代码来遍历列表元素,你就能高效地处理它们,即便列表包含数千万至数百万个元素。