您的位置:首页 > 编程语言 > Python开发

python——第4章操作列表

2020-01-31 23:33 501 查看

#4操作列表
#4.1 遍历整个列表
#需要对列表中的每个元素都执行相同的操作时,可使用python中的for()循环
magicians = [‘alice’,‘david’,‘carolina’]
for magician in magicians:
print(magician)
#每当名单长度发生变化时,都必须修改代码,通过使用for循环去处理这些问题

#1.对for循环中执行更多操作
#在for循环中可对每个元素执行任何操(如下指出表演太精彩)
magicians = [‘alice’,‘david’,‘carolina’]
for magician in magicians:
print(magician.title() + “,that was great trick!”)

#在for循环中,想包含多少代码都可以
#因此,可对列表中每一个值执行任意次数的操作
magicians = [‘alice’,‘david’,‘carolina’]
for magician in magicians:
print(magician.title() + “,that was great trick!”)
print(“I can’t wait to see your next trick,” + magician.title() + “.\n”)

#2.在for循环结束后执行一些操作
#想要在打印给各位魔术师的消息后面打印一条给全体魔术师的致谢消息,需要将相应的代码放在for循环后面,且不缩进
magicians = [‘alice’,‘david’,‘carolina’]
for magician in magicians:
print(magician.title() + “,that was great trick!”)
print(“I can’t wait to see your next trick,” + magician.title() + “.\n”)
print(“Thank you, everyone. That was a gerat magic show!”)
#使用for循环处理数据是一种对数据集执行整体操作的不错方式

#4.2 避免缩进错误(忘记缩进、忘记缩进额外的代码行、不必要的缩进、循环后不必要的缩进、遗漏冒号)
#for语句末尾的冒号告诉python,下一行是循环的第一行

#4.3 创建数字列表
#1、使用函数range() 使用它能够让你轻松生成一系列的数字
for value in range(1,5):
print(value)
#函数range()让python从你指定的第一个值开始,并在到达你指定的第二个值后停止,因此输出不包含第二个值(这里是5)
#使用函数range()时,如果输出不符合预期,请尝试将指定的值加1或减1

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

#使用函数range()时,还可以指定步长。例如,下面的代码打印1~10内的偶数
even_numbers = list(range(2,11,2))
print(even_numbers)
#函数range()从2开始数,然后不断加2,直到达到或超过终值(11)

#创建一个列表,其中包含前10个数(1-10)的平方,在python中表示乘方
squares = []
for value in range(1,11):
square = value
2
squares.append(square)
print(squares)

#3、对数字列表执行简单的统计计算
#轻松找出数字列表的最小值、最大值和和
digits = [1,2,3,4,5,6,7,8,9,0]
print(min(digits))
digits = [1,2,3,4,5,6,7,8,9,0]
print(max(digits))
digits = [1,2,3,4,5,6,7,8,9,0]
print(sum(digits))

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

#4.4 使用列表的一部分
#之前学习的是如何访问单个列表元素,学习了如何处理列别中的所有元素,你还可以处理列表部分元素————python称之为切片

#1、切片
#要创建切片,可指定要使用的第一个元素的索引和最后一个元素的索引加1.
players = [‘charles’,‘martina’,‘michael’,‘florence’,‘eli’]
print(players[0:3])
players = [‘charles’,‘martina’,‘michael’,‘florence’,‘eli’]
print(players[1:4])
players = [‘charles’,‘martina’,‘michael’,‘florence’,‘eli’]
print(players[:4])
players = [‘charles’,‘martina’,‘michael’,‘florence’,‘eli’]
print(players[2:])
players = [‘charles’,‘martina’,‘michael’,‘florence’,‘eli’]
print(players[-3:])

#2、遍历切片
#如果要遍历列表的部分元素,可在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())

#3、复制列表
#要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引[:]
#这让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_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)

#4.5元组
#列表非常适合于储存在程序运行期间可能变化的数据
#有时候你需要创建一系列不可修改的元素,元组可以满足这种需求
#python将不能修改的职称为 不可变的,而不可变的列表被称为 元组。

#1、定义元组
#元组看起来犹如列表,但使用圆括号()而不是方括号[]来标识。
#定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。
#(如果有一个大小不应该改变的矩形,可将其长度和宽度储存在一个元组中,而确保它们是不能被修改的)
dimensions = (200,50)
print(dimensions[0])
print(dimensions[1])
#此时第一行圆括号中的数值是不能被改变的

#2、遍历元组中分所有值
#想列表一样,也可以使用for循环来遍历元组中的所有值
dimensions = (200,50)
for dimension in dimensions:
print(dimension)

#3、修改元组变量
#虽然不能修改元组的元素,但可以给储存元组的变量赋值。因此,如果要修改前述矩形的尺寸,可重新定义整个元组
dimensions = (200,50)
print(“Original dimensions:”)
for dimension in dimensions:
print(dimension)

dimensions = (400,100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
#相比于列表,元组是更简单的数据结构。如果需要储存的一组在程序的整个生命周期内都不变,可使用元组。

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Sawoo_ 发布了3 篇原创文章 · 获赞 1 · 访问量 21 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: