【Python笔记】第4章 操作列表

本文介绍了Python中操作列表的技巧,包括使用for循环遍历、避免缩进错误、创建数字列表(如通过range()和list()函数)、列表切片与统计计算(如min、max和sum函数),以及如何正确地复制列表。此外,还简述了不可变的元组及其遍历方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.for语句遍历整个列表

需要对列表中的每个元素都执行相同的操作时,可以使用for循环。

magicians=['alice','david','carolina']
for magician in magicians:
    print(magician)
#alice
#david
#carolina

在for循环中,可对每个元素执行任何操作。每个缩进的代码行都是循环的一部分,且将针对列表中的每个值都执行一次。
在for循环后,没有缩进的代码都只执行一次,而不会重复执行。

magicians=['alice','david','carolina']
for magician in magicians:
    print(magician.title()+", that was a great trick!")
    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 trick!
I can't wait to see your next trick, Alice.

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

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

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

2.避免错误

忘记缩进;不必要的缩进;遗漏了冒号。

3.创建数字列表

3.1函数range()生成数字

range()函数生产一系列数字(经常差一行)
指定步长range(1,11,2) 这里2是步长。

for value in range(1,5):
    print(value)
1
2
3
4
for value in range(1,11,2):
    print(value)
1
3
5
7
9

3.2函数list()转换成列表

使用list()将range()的结果转换成列表。

numbers=list(range(1,6))
print(numbers)
#[1, 2, 3, 4, 5]
squares=[]

for value in range(1,11):
    square=value**2
    squares.append(square)
print(squares)
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

3.3对列表进行简单的统计计算

min()最小值
max()最大值
sum()总和

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

3.4列表解析

squares=[value**2 for value in range(1,11)]
print(squares)
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

4.使用列表的一部分

处理列表的部分元素,Python称之为切片。

4.1切片[ : ]

与range()一样,Python在到达指定第二个索引前面的元素后停止。

players=['charles','martina','micheal','florence','eli']
print(players[0:3])
#['charles', 'martina', 'micheal'] 

没有指定第一个索引,将自动从列表开头开始。
没有指定终止索引,将自动终止于列表末尾。

print(players[:4])
#['charles', 'martina', 'micheal', 'florence']
print(players[2:])
#['micheal', 'florence', 'eli']

负数索引返回离列表末尾相应距离的元素。

print(players[-3:])
#['micheal', 'florence', 'eli']

4.2遍历切片

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

players=['charles','martina','micheal','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
Micheal  

4.3复制列表

要复制列表,可创建一个包含整个列表的切片( : )

my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods[:]
print(my_foods)
print(friend_foods)
#['pizza', 'falafel', 'carrot cake']
#['pizza', 'falafel', 'carrot cake']

my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
#['pizza', 'falafel', 'carrot cake', 'cannoli']
#['pizza', 'falafel', 'carrot cake', 'ice cream']

不能使用简单的等于,两个变量都指向同一个列表。

my_foods=['pizza','falafel','carrot cake']
friend_foods=my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print(my_foods)
print(friend_foods)
#['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
#['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

5.元组

Python将不能修改的值称为不可变的,而不可变的列表称为元组。元组使用圆括号()而不是方括号[ ]。

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

也可以用for循环遍历所有值。

dimensions=(200,50)
for dimension in dimensions:
    print(dimension)
#200
#50
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值