Python编程:从入门到实践(三)

第四章 操作列表

学习如何遍历整个列表
循环让你能够对列表的每个元素都采取一个或一系列相同的措施

  1. 遍历整个列表
#使用for循环来打印魔术师名单中的所有名字
magicians = ['alice', 'david', 'carolina']
#for循环让Python从列表magicians中取出一个名字,并存储在变量magician中。
#对列表中每个元素,都将执行循环指定的步骤,而不管列表包含多少个元素
#另外,编写for循环时,对于用于存储列表中每个值的临时变量,可指定任何名称
#在for循环中,可对每个元素执行任何操作
#在代码行for...的后面,每个缩进的代码行都是循环的一部分,且将针对列表中的每个值都执行一次
for magician in magicians:
    print(magician)
    print(magician.title() + ", that was a great trick!")
#在for循环后面,没有缩进的代码都只执行一次,且不会重复执行
print("Thank you,everyone. That was a great magic show!")

输出:

alice
Alice, that was a great trick!
david
David, that was a great trick!
carolina
Carolina, that was a great trick!
Thank you,everyone. That was a great magic show!
  1. 避免缩进错误
    Python根据缩进来判断代码行与前一个代码行的关系
    Python通过使用缩进让代码更易读
    简单地说,它要求你使用缩进让代码整洁而结构清晰。在较长的Python程序中,你将看到缩进程度各不相同的代码块,这将让你对程序的组织结构有大致的认识
  2. 创建数值列表
#Python函数range()让你能够轻松地生成一系列的数字
#函数range()让Python从指定的第一个值开始数,并在到达你指定的第二个值后停止,因此输出不包括第二个值
for value in range(1, 5):
    print(value)

#要创建数字列表,可使用函数list()将range()的结果直接转换为列表
#如果将range()作为list()的参数,输出将为一个数字列表
numbers = list(range(1, 6))
print(numbers)

#使用函数range()时,还可指定步长
#在这个示例中,函数range()从2开始数,然后不断加2,直到达到或者超过终值11
even_numbers = list(range(2, 11, 2))
print(even_numbers)

#使用函数range()几乎能够创建任何需要的数字集
#下面的代码演示了如何将前10个整数的平方加入到一个列表中
#创建更复杂的列表时,可以使用上述两种方法中的任何一种
squares = []
for value in range(1, 11):
    square = value ** 2
    squares.append(square)
    #squares.append(value**2)
print(squares)

#有几个专门用于处理数字列表的Python函数
#例如以下,可轻松找出数字列表的最大值、最小值和总和
print(min(squares))
print(max(squares))
print(sum(squares))

#创建列表解析
#列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素
squares = [value ** 2 for value in range(1, 11)]
print(squares)

输出:

1
2
3
4
[1, 2, 3, 4, 5]
[2, 4, 6, 8, 10]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
1
100
385
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
  1. 使用列表的一部分
#处理列表的部分元素,Python称之为切片
#要创建切片,可指定要使用的第一个元素和最后一个元素的索引
#与函数range()一样,Python在到达指定的第二个索引前面的元素停止
#如果没有指定第一个索引,Python将自动从列表开头开始
#要让列表终止于列表末尾,使用类似的语法
#负数索引返回离列表末尾相应距离的元素
players = ['charles', 'martina', 'michale', 'florence', 'eli']
print(players[0:3])
print(players[1:3])
print(players[:3])
print(players[3:])
print(players[-3:])

#如果要遍历列表的部分元素,可在for循环中使用切片
print("Here are the first three players on my team: ")
for player in players[:3]:
    print(player.title())

#要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引(:)
#这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表
friends_players = players[:]
print(friends_players)
#为了核实确实有两个列表,在每个列表里添加不同的元素,打印这两个列表,核实不同元素确实包含在正确的列表中
players.append("maria")
friends_players.append("jane")
print(players)
print(friends_players)
#倘若我们只是简单地将friend_players赋给friend_players1,就不能得到两个列表
#这种语法实际上是让Python将新变量关联到包含旧变量中的列表,因此这两个变量都指向同一个列表
friends_players1 = friends_players
print(friends_players1)
friends_players.append("john")
friends_players1.append("hhhhh")
print(friends_players)
print(friends_players1)

输出:

['charles', 'martina', 'michale']
['martina', 'michale']
['charles', 'martina', 'michale']
['florence', 'eli']
['michale', 'florence', 'eli']
Here are the first three players on my team:
Charles
Martina
Michale
['charles', 'martina', 'michale', 'florence', 'eli']
['charles', 'martina', 'michale', 'florence', 'eli', 'maria']
['charles', 'martina', 'michale', 'florence', 'eli', 'jane']
['charles', 'martina', 'michale', 'florence', 'eli', 'jane']
['charles', 'martina', 'michale', 'florence', 'eli', 'jane', 'john', 'hhhhh']
['charles', 'martina', 'michale', 'florence', 'eli', 'jane', 'john', 'hhhhh']
  1. 元组
#Python将不能修改的值称为不可变的,而不可变得列表被称为元组
#相比于列表,元组是更简单的数据结构
#如果需要存储的一组值在程序的整个生命周期内都不变,可以使用元组
#元组看起来犹如列表,但使用圆括号而不是方括号来标识
#定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
#尝试修改第一个元素的值,导致Python返回错误消息
#dimensions[0] = 250
#TypeError: 'tuple' object does not support item assignment

#像列表一样,也可以使用for循环来遍历元组中的所有值
for dimension in dimensions:
    print(dimension)

#将一个新元组存储到变量dimensions中,然后,打印新的尺寸
#这次,Python不会报告任何错误,因为给元组变量赋值是合法的
dimensions = (400, 100)
for dimension in dimensions:
    print(dimension)
  1. 设置代码格式
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值