发布于2019-08-22 17:42 阅读(994) 评论(0) 点赞(23) 收藏(3)
遍历整个列表
使用for 循环来打印魔术师名单中的所有名字:
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
输出很简单,就是列表中所有的姓名:
alice
david
carolina
在for 循环中,可对每个元素执行任何操作。下面来扩展前面的示例,
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
下面的输出表明,对于列表中的每位魔术师,都打印了一条个性化消息:
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
在for 循环后面,没有缩进的代码都只执行一次,而不会重复执行。
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ", that was a great trick!")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show!")
你在前面看到了,开头两条print 语句针对列表中每位魔术师重复执行。然而,由于第三条print 语句没有缩进,因此只执行一次:
Alice, that was a great trick!
I can't wait to see your next trick, Alice.
David, that was a great trick!
I can't wait to see your next trick, David.
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina.
Thank you, everyone. That was a great magic show!
创建数值列表
Python函数range() 让你能够轻松地生成一系列的数字。
for value in range(1,5):
print(value)
上述代码好像应该打印数字1~5,但实际上它不会打印数字5:
1
2
3
4
要创建数字列表,可使用函数list() 将range() 的结果直接转换为列表。如果将range() 作为list() 的参数,输出将为一个数字列表。
numbers = list(range(1,6))
print(numbers)
结果如下:
[1, 2, 3, 4, 5]
使用函数range() 时,还可指定步长。例如,下面的代码打印1~10内的偶数:
even_numbers = list(range(2,11,2))
print(even_numbers)
在这个示例中,函数range() 从2开始数,然后不断地加2,直到达到或超过终值(11),因此输出如下:
[2, 4, 6, 8, 10]
在Python中,两个星号(** )表示乘方运算。下面的代码演示了如何将前10个整数的平方加入到一个列表中:
squares = []
for value in range(1,11):
square = value**2
squares.append(square)
print(squares)
首先,我们创建了一个空列表;接下来,使用函数range() 让Python遍历1~10的值。在循环中,计算当前值的平方,并将结果存储到变量square 中。然后,将新计算得到的平方值附加到列表squares 末尾。最后,循环结束后,打印列表squares :
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
下面的示例使用列表解析创建你在前面看到的平方数列表:
squares = [value**2 for value in range(1,11)]
print(squares)
要使用这种语法,首先指定一个描述性的列表名,如squares ;然后,指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value2 ,它计算平方值。接下来,编写一个for 循环,用于给表达式提供值,再加上右方括号。在这个示例中,for 循环为for value in range(1,11) ,它将值1~10提供给表达式value2 。请注意,这里的for 语句末尾没有冒号。
结果与你在前面看到的平方数列表相同:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
使用列表的一部分
要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range() 一样,Python在到达你指定的第二个索引前面的元素后停止。要输出列表中的前三个元素,需要指定索引0~3,这将输出分别为0 、1 和2 的元素。
下面的示例处理的是一个运动队成员列表:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
其中只包含三名队员。输出也是一个列表,其中包含前三名队员:
['charles', 'martina', 'michael']
如果你没有指定第一个索引,Python将自动从列表开头开始:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
由于没有指定起始索引,Python从列表开头开始提取:
['charles', 'martina', 'michael', 'florence']
要让切片终止于列表末尾,也可使用类似的语法。例如,如果要提取从第3个元素到列表末尾的所有元素,可将起始索引指定为2 ,并省略终止索引:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
Python将返回从第3个元素到列表末尾的所有元素:
['michael', 'florence', 'eli']
如果你要输出名单上的最后三名队员,可使用切片players[-3:] :
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
上述代码打印最后三名队员的名字,即便队员名单的长度发生变化,也依然如此。
如果要遍历列表的部分元素,可在for 循环中使用切片。在下面的示例中,我们遍历前三名队员,并打印他们的名字:
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
代码没有遍历整个队员列表,而只遍历前三名队员:
Here are the first three players on my team:
Charles
Martina
Michael
要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:] )。这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表。
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']
定义元组
元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
接下来,我们分别打印该元组的各个元素,使用的语法与访问列表元素时使用的语法相同:
200
50
像列表一样,也可以使用for 循环来遍历元组中的所有值:
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
就像遍历列表时一样,Python返回元组中所有的元素:
200
50
作者:齐天大圣
链接:https://www.pythonheidong.com/blog/article/53246/bf8fbddc89c6e15450dc/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!