Python编程笔记,第4记:操作列表
本节将学习的是如何操作列表,包括:如何用for循环遍历列表、如何创建数字列表、如何通过切片来使用列表的一部分和复制列表、元组……
目录
# 1.使用range()函数,在Python中能够轻易地生成一系列的数字.
# 4.如何创建一个列表,其中包含前10个整数(1~10)的平方.在Python中两个星号**表示乘方运算.
# 1.切片:只处理列表的部分元素,Python称之为切片.
# 4.操作列表
# 4.1遍历整个列表,可以用for循环来实现.
# 4.1遍历整个列表,可以用for循环来实现.
magicians = ['alice', 'oliver', 'selene', 'david']
for magician in magicians: # 特别要注意该处的冒号不能掉.否侧会导致语法错误.
print(magician)
# 从列表magicians中取出一个元素,存储到临时变量magician中,然后将其打印输出.
# 使用单数和复数形式的名称,有助于判断代码处理的是单个列表元素还是整个列表.
输出结果为:
alice
oliver
selene
david
- # 使用单数和复数形式的名称,有助于判断代码处理的是单个列表元素还是整个列表.
- # 在for循环中,对列表中的每个元素执行相同操作.
# 使用单数和复数形式的名称,有助于判断代码处理的是单个列表元素还是整个列表.
# 在for循环中,对列表中的每个元素执行相同操作.
print("\n") # 只是为了换行.
magicians = ['alice', 'oliver', 'carolina', 'david']
for magician in magicians:
print(magician.title() + ", that was a great tick.")
print("I can't wait to see your next trick, " + magician.title() + ".\n")
print("Thank you, everyone. That was a great magic show.")
输出结果为:
Alice, that was a great tick.
I can't wait to see your next trick, Alice.
Oliver, that was a great tick.
I can't wait to see your next trick, Oliver.
Carolina, that was a great tick.
I can't wait to see your next trick, Carolina.
David, that was a great tick.
I can't wait to see your next trick, David.
Thank you, everyone. That was a great magic show.
# 4.2避免缩进错误.
- # Python根据缩进来判断代码行与前一个代码行的关系.在for循环后面,每个缩进的代码行都是循环的一部分,会针对列表中的值都执行一次;
- # 没有缩进的代码行都只会执行一次,而不会重复执行.
# 4.3创建数值列表
# 1.使用range()函数,在Python中能够轻易地生成一系列的数字.
# 4.3创建数值列表
# 1.使用range()函数,在Python中能够轻易地生成一系列的数字.
print("\n") # 只是为了换行.
for value in range(1, 5): # 再次提醒冒号:一定不能掉.
print(value)
# 函数range()让Python从指定的第一个值开始数,并在到达指定的第二个值后停止,因此输出不包含你第二个值.
- # 函数range()让Python从指定的第一个值开始数,并在到达指定的第二个值后停止,因此输出不包含你第二个值.
输出结果为:
1
2
3
4
# 2.使用range()创建数字列表.
- # 要创建数字列表,可使用函数list()将range()的结果直接转换为列表.
- # 如果将range()作为函数list()的参数,输出将为一个数字的列表.
# 2.使用range()创建数字列表.
# 要创建数字列表,可使用函数list()将range()的结果直接转换为列表.
# 如果将range()作为函数list()的参数,输出将为一个数字的列表.
numbers = list(range(1, 6))
print(numbers)
输出结果为:
[1, 2, 3, 4, 5]
# 3.使用函数range()还可以指定步长.
# 3.使用函数range()还可以指定步长.
even_numbers = list(range(2, 11, 2))
print(even_numbers)
# 解析:函数range()从2开始数,然后不断的加2,直到达到或超过终值(11).
- # 解析:函数range()从2开始数,然后不断的加2,直到达到或超过终值(11).
输出结果为:
[2, 4, 6, 8, 10]
# 4.如何创建一个列表,其中包含前10个整数(1~10)的平方.在Python中两个星号**表示乘方运算.
# 4.如何创建一个列表,其中包含前10个整数(1~10)的平方.在Python中两个星号**表示乘方运算.
squares = [] # 创建一个空列表.
# 函数range()参数中指定的数值范围不包括第二个值,所以生成数字1~10,range()函数的第二个参数是11.
for value in range(1, 11):
square = value**2
squares.append(square) # 将元素square添加到列表squares的末尾.
print(squares)
# 为了让上面的代码更简洁,也可以不使用临时变量square.
print("The code is more concise:")
squares = [] # 创建一个空列表.
for value in range(1, 11): # 函数range()参数中指定的数值范围不包括第二个值,所以生成数字1~10,range()函数的第二个参数是11.
squares.append(value**2) # 将元素value**2添加到列表squares的末尾.
print(squares)
# 有时候,使用临时变量可以让代码更易读;但有时候,这样做只会让代码无谓地变长.
- # 有时候,使用临时变量可以让代码更易读;但有时候,这样做只会让代码无谓地变长.
输出结果为:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
The code is more concise:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 5.对数字列表进行简单的统计计算.
# 5.对数字列表进行简单的统计计算.
digits = []
for digit in range(0, 10):
digits.append(digit)
print("\nThe digits is:")
print(digits)
print("The min value in the digits:")
print(min(digits)) # 求列表中的最小值.
print("The max value in the digits:")
print(max(digits)) # 求列表中的最大值.
print("The sum of all the numbers in the digits:")
print(sum(digits)) # 求数字列表中所有元素的和.
输出结果为:
The digits is:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The min value in the digits:
0
The max value in the digits:
9
The sum of all the numbers in the digits:
45
# 6.列表解析
- # 列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素.
# 6.列表解析
# 列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素.
print("\n列表解析 value**2:")
squares = [value**2 for value in range(1, 11)] # 请注意啦:这里的for语句末尾没有冒号.
print(squares)
输出结果为:
列表解析 value**2:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# 4.4使用列表的一部分
# 1.切片:只处理列表的部分元素,Python称之为切片.
- # 要创建切片,可指定要使用的第一个元素和最后一个元素的索引.
- # 与range()函数一样,Python在到达指定的第二个索引前面的元素后停止,即不包含第二个索引元素.
# 4.4使用列表的一部分
# 1.切片:只处理列表的部分元素,Python称之为切片.
# 要创建切片,可指定要使用的第一个元素和最后一个元素的索引.
# 与range()函数一样,Python在到达指定的第二个索引前面的元素后停止,即不包含第二个索引元素.
players = ['tiny', 'coco', 'sven', 'viper', 'mortred', 'lina']
print("\nHere is the source list:")
print(players)
print("切片:第1~3个元素:")
print(players[0:3]) # 打印出第1~3个元素.
print('切片:第2~4个元素:')
print(players[1:4])
# 如果没有指定第一个索引,Python将自动从列表开头开始.
print("切片:没有指定第一个索引:")
print(players[:3])
# 要让切片终止于末尾,也可以使用类似的语法.
print("切片:没有指定第二个索引:")
print(players[2:])
# 负数索引返回离列表末尾相应距离的元素,因此也可以输出列表末尾的任何切片.
print("末尾切片:")
print(players[-3:])
输出结果为:
Here is the source list:
['tiny', 'coco', 'sven', 'viper', 'mortred', 'lina']
切片:第1~3个元素:
['tiny', 'coco', 'sven']
切片:第2~4个元素:
['coco', 'sven', 'viper']
切片:没有指定第一个索引:
['tiny', 'coco', 'sven']
切片:没有指定第二个索引:
['sven', 'viper', 'mortred', 'lina']
末尾切片:
['viper', 'mortred', 'lina']
# 2.遍历切片
# 2.遍历切片
players = ['tiny', 'coco', 'sven', 'viper', 'mortred', 'lina']
print("\nHere are the first three players on my team:")
for player in players[:3]: # 再次提醒,for循环语句最后的冒号:不能掉.否侧会有语法错误.
print(player.title())
输出结果为:
Here are the first three players on my team:
Tiny
Coco
Sven
# 3.复制列表
- # 要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引 [:]。
- # 这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表.
# 3.复制列表
# 要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引 [:]。
# 这让Python创建一个始于第一个元素,终止于最后一个元素的切片,即复制整个列表.
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("\nMy favorite foods are:")
print(my_foods)
print("My friend's favorite foods are:")
print(friend_foods)
my_foods.append("cannoli")
friend_foods.append("ice cream")
print("\nMy new favorite foods are:")
print(my_foods)
print("My friend's new 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 new favorite foods are:
['pizza', 'falafel', 'carrot cake', 'cannoli']
My friend's new favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
- # 这里是将my_foods赋值给friend_foods,而不是将副本存储到friend_foods.因此这两个变量指向的是同一个列表.
# 这里是将my_foods赋值给friend_foods,而不是将副本存储到friend_foods.因此这两个变量指向的是同一个列表.
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods
friend_foods.append("ice cream")
print("\n2My favorite foods are:")
print(my_foods)
print("2My friend's favorite foods are:")
print(friend_foods)
# 由于my_foods和friend_foods这两个变量指向的是同一个列表,所以改变任一个变量中的内容,都会改变另外一个.
- # 由于my_foods和friend_foods这两个变量指向的是同一个列表,所以改变任一个变量中的内容,都会改变另外一个.
输出结果为:
2My favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
2My friend's favorite foods are:
['pizza', 'falafel', 'carrot cake', 'ice cream']
# 4.5元组
- # Python将不能修改的值称为不可变的,而不可变的列表被称为元组.
- # 元组看起来犹如列表,但是使用圆括号而不是方括号来标识.
# 4.5元组
# Python将不能修改的值称为不可变的,而不可变的列表被称为元组.
# 元组看起来犹如列表,但是使用圆括号而不是方括号来标识.
dimensions = (200, 50)
print("\n元组.")
print(dimensions[0])
print(dimensions[1])
输出结果为:
元组.
200
50
# 1.遍历元组中的所有值.
- # 像列表一样,可以使用for循环来遍历元组中的所有值.
# 1.遍历元组中的所有值.
# 像列表一样,可以使用for循环来遍历元组中的所有值.
dimensions = (2000, 5000, 1000, 2200)
for dimension in dimensions: # 再啰嗦一遍,for循环语句最后的冒号:不能掉.否侧会有语法错误.
print(dimension)
输出结果为:
2000
5000
1000
2200
# 修改元组变量
- # 虽然不能修改元组的元素,但可以给存储元组的变量重新赋值.
- # 如果试图修改元组中元素的值,将会报错.因为Python禁止修改元组中元素的值.但可以给存储元组的变量赋值.
- # 如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组.
# 修改元组变量
# 虽然不能修改元组的元素,但可以给存储元组的变量重新赋值.
dimensions = (600, 300)
for dimension in dimensions:
print(dimension)
dimensions = (800, 500)
for dimension in dimensions:
print(dimension)
# 如果试图修改元组中元素的值,将会报错.因为Python禁止修改元组中元素的值.但可以给存储元组的变量赋值.
# 如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组.
输出结果为:
600
300
800
500
-
宝剑锋从磨砺出,梅花香自苦寒来.