每日Python五分钟3:操作列表

版权声明:本文为博主原创文章,转载请标明地址。欢迎关注微信订阅号:PM实验室。 https://blog.csdn.net/plain_maple/article/details/89211192
欢迎关注微信公众号:PM实验室

for循环遍历列表
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
    print(magician)

输出

alice
david
carolina

Python根据缩进来判断代码行与前一代码行的关系。上例中,print这一行只有缩进了才是for循环的一部分。不必要的缩进不能缩进,必要的缩进必须缩进。

for value in range(1, 5):
    print(value)

输出

1
2
3
4

函数range()让Python从你指定的第一个值开始数,并在到达你指定的第二个值后停止。输出不包含第二个值(这里为5)。

使用range()创建数字列表

numbers = list(range(1, 6))
print(numbers)

输出

 [1, 2, 3, 4, 5]

range()指定步长

even_numbers = list(range(5, 13, 2))
print(even_numbers)

输出

[5, 7, 9, 11]

从5开始,不断地加2,直到达到或超过终值13。

前10个整数的平方加入到一个列表中

squares = []
for value in range(1, 11):
    square = value**2  # **表示乘方运算,**2即value的平方
    squares.append(square)
print(squares)

输出

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

简单的统计计算

digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print(min(digits))
print(max(digits))
print(sum(digits))

输出

0
9
45

列表解析(一行代码就可以生成上述的squares列表)

squares = [value**2 for value in range(1, 11)]
print(squares)

输出

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

for循环为for value in range(1,11),它将值1~10提供给表达式value**2

学习了如何访问单个列表元素,也学习了如何处理列表的所有元素。Python还可以处理列表的部分元素——Python称之为切片。

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3]) # 0:3是起始索引:结束索引

输出

['charles', 'martina', 'michael']

又如

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])

输出

['martina', 'michael', 'florence']

没有指定第一个索引, Python将自动从列表开头开始

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])

输出

['charles', 'martina', 'michael', 'florence']

同理

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])

输出

['michael', 'florence', 'eli']

换个负数索引

players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])

输出

['michael', 'florence', 'eli']

遍历切片

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

复制列表

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']

追加food

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']

元组(不可变的列表)

dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])

输出

200
50

遍历元组

dimensions = (200, 50)
for dimension in dimensions:
    print(dimension)

输出

200
50

修改元组变量(虽然不能修改元组的元素,但可以给存储元组的变量赋值。)

dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
    print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

输出

Original dimensions:
200
50

Modified dimensions:
400
100

猜你喜欢

转载自blog.csdn.net/plain_maple/article/details/89211192