ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【笔记】Python | 04 | 操作列表 | 4.4 使用列表的一部分

2022-02-19 12:04:23  阅读:255  来源: 互联网

标签:4.4 04 favorite 列表 foods print pizzas friend


使用列表的一部分

通过遍历我们可以使用列表的全部元素,有时我们仅需要使用列表里的一部分元素,此时就要用到切片。

切片

要使用切片,指定要使用的第一个元素和最后一个元素的索引。与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时,两个变量将指向同一个列表,对两个变量增加元素,其实就是在对同一个列表增加元素。

练习题

4-10 切片

选择一个列表,在末尾添加几行代码,完成以下任务:

  1. 打印消息"The first three items in the list are:",再使用切片来打印列表的前三个元素。
  2. 打印消息"Three items from the middle of the lists are:",再使用切片来打印列表中间的三个元素、
  3. 打印消息"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-11 你的披萨和我的披萨

在练习4-1中,创建披萨列表的副本,并将其存储到标量friend_pizzas中,再完成如下任务。

  1. 在原来的披萨列表中添加一种披萨。
  2. 在列表friend_pizzas中添加另一种披萨。
  3. 核实你有两个不同的列表。为此打印消息"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

4-12 使用多个循环

在本节中,为节省篇幅,程序foods.py的每个版本都没有使用for循环来打印列表。请选择一个版本的foods.py,在其中表写两个for循环,将各个食品列表都打印出来。

# 略。

标签:4.4,04,favorite,列表,foods,print,pizzas,friend
来源: https://www.cnblogs.com/bob-log/p/15912176.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有