Python 列表的操作

遍历整个列表

for循环

magicians = ['alice','david','carolina']
for magician in magicians:
	print(magician)
	
输出:
alice
david
carolina
在for循环中执行更多操作(缩进)
magicians = ['alice','david','carolina']
for magician in magicians:
	print(f"{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(f"{magician.title()},that was a great trick!")
	print(f"I can't to see your next trick, {magician.title()}.\n")
print("Thank you, everyone.That was a great magic show!")

输出:
Alice,that was a great trick!
I can't to see your next trick, Alice.

David,that was a great trick!
I can't to see your next trick, David.

Carolina,that was a great trick!
I can't to see your next trick, Carolina.

Thank you, everyone.That was a great magic show!

避免缩进错误

Python 根据缩进来判断代码行与前一个代码行的关系。

创建数值列表

使用函数range()

函数range()让Python从指定的第一个值开始数,并在到达你指定的第二个值时停止。因为它在第二个值处停止,所以输出不包含该值。range(1,5)只打印1、2、3、4

for value in range(1,5):
	print(value)
	
输出:
1
2
3
4

调用函数range()时,也可只指定一个参数,这样他将从0开始。range(6)返回0-5

使用range()创建数字列表

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

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

输出:[1, 2, 3, 4, 5]

使用函数range()时,还可指定步长。为此,可给这个函数指定第三个参数,Python将根据这个步长来生成数。
如下,函数range()从2开始数,然后不断加2,直到达到或超过终值(11)

numbers = list(range(2,11,2))
print(numbers)

输出:[2, 4, 6, 8, 10]

乘方运算

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

输出:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
对数字列表执行简单的统计计算

有几个专门用于处理数字列表的Python函数。最大值、最小值、总和

digits = list(range(10))
print(min(digits))
print(max(digits))
print(sum(digits))

输出:
0
9
45
列表解析

列表解析将for循环和创建新元素的代码合并成为一行,并自动附加新元素

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

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

使用列表的一部分

切片

要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range()一样,Python在到达第二个索引之前的元素后停止。
如果没有指定第一个索引,Python将自动从列表开头开始

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

输出:
['charles', 'martina', 'michael']
['martina', 'michael', 'florence']
['charles', 'martina', 'michael', 'florence']

要让切片终止于列表末尾,也可以使用类似的语法。指定起始索引,并省略终止索引

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

负数索引返回离列表末尾相应距离的元素,因此你可以输出列表末尾的任意切片,例如:players[-3:]

注:可在表示切片的方括号内指定第三个值。这个值告诉python在指定范围内每隔多少元素可以提取一个。

遍历切片

遍历列表中的部分元素,可以在for循环中使用切片。

players = ['charles','martina','michael','florence','eli']
print("Here are the first three players on my team:")
for player in players:
    print(player.title())
    
输出:
Here are the first three players on my team:
Charles
Martina
Michael
复制列表

要复制列表,可创建一个包含整个列表的切片,方法是同时省略起始索引和终止索引([:])

my_food = ['pizza','falafel','carrot cake']
myfriend_food = my_food[:]#my_food的副本

myfriend_food = my_food #此时两个变量指向同一个列表

元组

不可变的列表,使用圆括号定义元组。

遍历元组中的所有值

与遍历列表类似

dimensions = (200,50)
for dimension in dimensions:
    print(dimension)
修改元组变量

虽然不能修改元组的元素,但可以给存储元组的变量赋值,可重新定义整个元组。

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

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

Modified dimensions:
400
250
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值