目录
第3章 列表简介
3.1 列表是什么
列表由一系列按特定顺序排列的元素组成。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
3.1.1 访问列表元素
列表是有序集合,因此要访问列表的任意元素,只需将该元素的的位置(索引)告诉Python即可。
print(bicycles[0])
trek
print(bicycles[0].title())
Trek
3.1.2 索引从0开始而不是1开始
print(bicycles[1])
cannondale
print(bicycles[-1])
specialized
3.1.3 使用列表中的各个值
message = f'My first bicycle was a {bicycles[0].title()}.'
print(message)
My first bicycle was a Trek.
3.2 修改、添加和删除元素
3.2.1 修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['ducati', 'yamaha', 'suzuki']
3.2.2 在列表中添加元素
01 在列表末尾添加元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha', 'suzuki', 'ducati']
02 在列表中插入元素
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
3.2.3 列表中删除元素
01 使用del语句删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles)
['yamaha', 'suzuki']
02 使用方法pop()删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
['honda', 'yamaha']
suzuki
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print(f'The last motorcycle I owned was a {last_owned.title()}.')
The last motorcycle I owned was a Suzuki.
03 弹出列表中任何位置处的元素
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print(f'The first motorcycle I owned was a {first_owned}.')
print(motorcycles)
The first motorcycle I owned was a honda.
['yamaha', 'suzuki']
每当使用pop()时,被弹出的元素就不再在列表中了。
如果你要从列表中删除一个元素,且不再以任何方式使用它,就是用del语句;如果你要在删除元素后还能继续使用它,就是用方法pop()。
04 根据值删除元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.remove('honda')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
['yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
too_expensive = 'suzuki'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f'\nA {too_expensive.title()} is too expensive for me.')
['honda', 'yamaha', 'suzuki']
['honda', 'yamaha']
A Suzuki is too expensive for me.
方法remove()只删除第一个指定的值,如果要删除的值可能在列表中出现多次,就需要使用循环来确保每个值都删除。
3.3 组织列表
3.3.1 使用方法sort()对列表永久排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
['audi', 'bmw', 'subaru', 'toyota']
方法sort()永久性地修改列表元素的排列顺序。
还可以按字母顺序相反的顺序排列列表元素。
cars.sort(reverse=True)
print(cars)
['toyota', 'subaru', 'bmw', 'audi']
3.3.2 使用函数sorted()对列表临时排序
函数sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print('Here is the original list:')
print(cars)
print('\n Here is the sorted list:')
print(sorted(cars))
print(sorted(cars, reverse=True))
print('\n Here is the original list again:')
print(cars)
Here is the original list:
['bmw', 'audi', 'toyota', 'subaru']
Here is the sorted list:
['audi', 'bmw', 'subaru', 'toyota']
['toyota', 'subaru', 'bmw', 'audi']
Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
3.3.3 倒着打印列表
要反转列表元素的排列顺序,可使用方法reverse()。reverse()不是按与字母顺序相反的顺序排列列表元素,而只是反转列表元素的排列顺序。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
方法reverse()永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,只需对列表再次调用reverse()即可。
3.3.4 确定列表的长度
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
4
3.4 使用列表时避免索引错误
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])
IndexError: list index out of range
motorcycles = []
print(motorcycles[-1])
IndexError: list index out of range