列表
1、列表是什么
列表由一系列按特定顺序排列的元素组成。你可以创建包含字母表中所有字母、数字0~9或所有家庭成员姓名的列表;也可以将任何东西加入列表中,其中的元素之间可以没有任何关系。 鉴于列表通常包含多个元素,给列表指定一个表示复数的名称(如letters、digits或names)是个不错的主意。
在Python中,用方括号([])来表示列表,并用逗号来分隔其中的元素。
2、访问列表元素
列表是有序集合,因此要访问列表的任何元素,只需将该元素的位置或索引告诉Python即可。
要访问列表元素,可指出列表的名称,再指出元素的索引,并将其放在方括号内。
- 在Python中,第一个列表元素的索引为0,而不是1。
- 在Python中,通过将索引指定为-1,可返回最后一个列表元素。这种约定也适用于其他负数索引。
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
#trek
print(bicycles[1])
#cannondale
print(bicycles[-1])
#specialized
print(bicycles[-2])
#redline
message = "My first bicycle was a " + bicycles[0].title() + "."
print(message)
#My first bicycle was a Trek.
3、修改、添加和删除元素
修改列表元素
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
motorcycles[0] = 'ducati'
print(motorcycles)
#['ducati', 'yamaha', 'suzuki']
在列表中添加元素
- 在列表末尾添加元素
方法append()将元素’ducati’添加到了列表末尾,而不影响列表中的其他所有元素:
为控制用户,可首先创建一个空列表,用于存储用户将要输入的值,然后用append()将用户提供的每个新值附加到列表中。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
#['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
- 在列表中插入元素
方法insert()可在列表的任何位置添加新元素。为此,需要指定新元素的索引和值。
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
#['ducati', 'honda', 'yamaha', 'suzuki']
删除列表元素
- 使用del语句删除元素
del语句可删除任何位置处的列表元素,前提是知道其索引
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
del motorcycles[0]
print(motorcycles)
#['yamaha', 'suzuki']
使用del语句将数值从列表中删除后,你就无法再访问它了
- 使用方法pop()删除元素
方法pop()可删除列表末尾的元素,并让你能够接着使用它。
术语弹出(pop)源自这样的类比:列表就像一个栈,而删除列表末尾的元素相当于弹出栈顶元素。
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
popped_motorcycle = motorcycles.pop()
print(motorcycles)
#['honda', 'yamaha']
print(popped_motorcycle)
#suzuki
- 弹出列表中任何位置处的元素
你可以使用pop()来删除列表中任何位置的元素,只需在括号中指定要删除的元素的索引
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned.title() + '.')
#The first motorcycle I owned was a Honda.
如果你要从列表中删除一个元素,且不再以任何方式使用它,就使用del语句;如果你要在删除元素后还能继续使用它,就使用方法pop();
- 根据值删除元素
不知道要从列表中删除的值所处的位置。如果你只知道要删除的元素的值,可使用方法remove()
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
#['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles.remove('ducati')
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
使用remove()从列表中删除元素时,也可接着使用它的值。
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
#['honda', 'yamaha', 'suzuki', 'ducati']
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
#['honda', 'yamaha', 'suzuki']
print("\nA " + too_expensive.title() + " is too expensive for me.")
#
#A Ducati is too expensive for me.
方法:remove()只删除第一个指定的值。如果要删除的值可能在列表中出现多次,就需要使用循环来判断是否删除了所有这样的值。
4、组织列表
使用方法sort()对列表进行永久性排序
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
#['audi', 'bmw', 'subaru', 'toyota']
此时,汽车是按字母顺序排列的,再也无法恢复到原来的排列顺序。
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)
#['toyota', 'subaru', 'bmw', 'audi']
此时,将汽车列表按与字母顺序相反的顺序排列,再也无法恢复到原来的排列顺序。
使用函数sorted()对列表进行临时排序
函数 sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。
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 sorted list(reverse):")
print(sorted(cars,reverse=True))
print("\nHere 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']
Here is the sorted list(reverse):
['toyota', 'subaru', 'bmw', 'audi']
Here is the original list again:
['bmw', 'audi', 'toyota', 'subaru']
倒着打印列表
要反转列表元素的排列顺序,可使用方法reverse()。
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru']
['subaru', 'toyota', 'audi', 'bmw']
方法reverse()永久性地修改列表元素的排列顺序,但可随时恢复到原来的排列顺序,为此 只需对列表再次调用reverse()即可。
确定列表的长度
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(len(cars))
# 4