通过遍历我们可以使用列表的全部元素,有时我们仅需要使用列表里的一部分元素,此时就要用到切片。
要使用切片,指定要使用的第一个元素和最后一个元素的索引。与range()
一样,Python会在达到第二个元素之前停止。要输出前3个元素,需要指定索引0-3——输出索引分别为0,1,2的元素。
players = ['charles', 'martina', 'michael', 'florence', 'eli'] print(players[0:3]) print(players[1:3])
输出结果:
>>> ['charles', 'martina', 'michael'] ['martina', 'michael']
如果没有指定第一个索引,Python将默认从头开始:
print(players[:2])
输出结果:
>>> ['charles', 'martina']
如果没有指定第二个索引,默认截取到最后一个元素:
print(players[2:])
输出结果:
>>> ['michael', 'florence', 'eli']
负数索引返回距离末尾相应距离的元素,输出最后三个人:
print(players[-3:])
输出结果:
>>> ['michael', 'florence', 'eli']
如果要遍历列表的部分元素,可在for循环中使用切片。
遍历前3名队员,并打印出来
print("Here are the first three players on my team:") for player in players[0:3]: print(player.title())
输出结果:
>>> Here are the first three players on my team: Charles Martina Michael
有时需要根据既有列表创建新的列表。复制列表,就创建一个包含整个列表的切片。方法是同时省略起始索引和终止索引([:]
)。P原合同会创建一个始于第一个元素、终止于最后一个元素的切片,即复制列表。
假设有一个食物列表,包含了你喜欢的食物。你想再创建一个列表,里面包含了你喜欢的食物之外,还包含你朋友喜欢的食物。因此可以先复制你的列表,再加上你朋友喜欢的其他食物。
my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)
输出结果:
>>> My favorite foods are: ['pizza', 'falafel', 'carrot cake'] My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake']
从结果看到,两个列表相同,即复制成功。
现在我们在两个列表中分别添加一个元素,再看看这两个列表的内容:
my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods[:] my_foods.append('cannoli') friend_foods.append('ice cream') print("My favorite foods are:") print(my_foods) print("\nMy friend's favorite foods are:") print(friend_foods)
输出结果:
>>> My favorite foods are: ['pizza', 'falafel', 'carrot cake', 'cannoli'] My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake', 'ice cream']
我的食物与朋友的食物确实不一样。
在不使用切片的情况下尝试复制列表的情况:
my_foods = ['pizza', 'falafel', 'carrot cake'] friend_foods = my_foods my_foods.append('cannoli') friend_foods.append('ice cream') print(">>> My favorite foods are:") print(my_foods) print("\n>>> My friend's favorite foods are:") print(friend_foods)
输出结果:
>>> My favorite foods are: ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream'] >>> My friend's favorite foods are: ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
此时两个列表输出结果是一样的。简单的解释就是,my_foods
赋值给friend_foods
时,两个变量将指向同一个列表,对两个变量增加元素,其实就是在对同一个列表增加元素。
选择一个列表,在末尾添加几行代码,完成以下任务:
"The first three items in the list are:"
,再使用切片来打印列表的前三个元素。"Three items from the middle of the lists are:"
,再使用切片来打印列表中间的三个元素、"The last three items in the list are:"
,再使用切片来打印列表末尾的三个元素。letters = ['A','B','C','D','E','F','G'] print("The first three items in the list are:") print(letters[:3]) print("Three items from the middle of the lists are:") print(letters[2:5]) print("The last three items in the list are:") print(letters[-3:])
输出结果:
>>> The first three items in the list are: ['A', 'B', 'C'] Three items from the middle of the lists are: ['C', 'D', 'E'] The last three items in the list are: ['E', 'F', 'G']
在练习4-1中,创建披萨列表的副本,并将其存储到标量friend_pizzas
中,再完成如下任务。
friend_pizzas
中添加另一种披萨。"My favorite pizzas are:"
,再使用一个for循环来打印第一个列表:打印消息"My friend's favorite pizzas are:"
,再使用一个for循环来打印第二个列表。核实新增的披萨被添加到正确的列表中。pizzas = ["Napoletana","Taglio","Sfincione","Calzone","Pizzolo"] print("\nMy favorite pizzas are:") print(pizzas) pizzas.append("Padellino") print("\nMy favorite pizzas are:") print(pizzas) friend_pizzas = pizzas[:] friend_pizzas.append("Scaccia") print("\nMy friend's favorite pizzas are:") print(friend_pizzas) print("\nMy favorite pizzas are:") print(pizzas) print("\n") for pizza in pizzas: print(pizza) print("\n") for pizza in friend_pizzas: print(pizza)
输出结果:
>>> My favorite pizzas are: ['Napoletana', 'Taglio', 'Sfincione', 'Calzone', 'Pizzolo'] My favorite pizzas are: ['Napoletana', 'Taglio', 'Sfincione', 'Calzone', 'Pizzolo', 'Padellino'] My friend's favorite pizzas are: ['Napoletana', 'Taglio', 'Sfincione', 'Calzone', 'Pizzolo', 'Padellino', 'Scaccia'] My favorite pizzas are: ['Napoletana', 'Taglio', 'Sfincione', 'Calzone', 'Pizzolo', 'Padellino'] Napoletana Taglio Sfincione Calzone Pizzolo Padellino Napoletana Taglio Sfincione Calzone Pizzolo Padellino Scaccia
在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for循环来打印列表。请选择一个版本的foods.py,在其中表写两个for循环,将各个食品列表都打印出来。
# 略。