[Python学习笔记2]——列表的进阶操作

目录

1. 使用for循环进行列表的遍历

2. 创建数值列表

2.1 使用函数range()

2.2 使用函数range()和函数list()创建数值列表

3. 列表解析

4. 列表的切片

4.1 切片

4.2 遍历切片

4.3 复制列表(区分切片复制与直接赋值)

5. 使用if语句判断列表是否非空

6. 使用关键字in或not in检查特定元素是否在列表中


1. 使用for循环进行列表的遍历

假设我们有一个魔术师名单,需要将其中每个魔术师的名字都打印出来,则可以用for 循环进行遍历并打印,如:

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

这段代码从列表magicians中取出一个名字,并将其存储在变量magician中,进而将其打印,其输出结果如下:

alice 
david 
carolina

2. 创建数值列表

2.1 使用函数range()

使用函数range()可以生成一些数字,如:

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

则打印的结果为:

1 
2 
3 
4

需要注意的是,range(1,5)只是打印数字1~4,并且range(1,5)并不会生成一个包含数字1~4的列表。

2.2 使用函数range()和函数list()创建数值列表

上面提到了使用range()并不会生成列表,要相将其结果直接转换为列表,可以使用函数list(),如:

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

则此时的输出结果为一个包含数字1~5的数值列表:

[1, 2, 3, 4, 5]

使用函数range()时,还可指定步长。例如,下面的代码打印1~10内的偶数:

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

在这段代码中,函数range()从2开始数,然后不断地加2,直到达到或超过终值11,因此输出如下:

[2, 4, 6, 8, 10]

3. 列表解析

列表解析将for循环和创建新元素的代码合并成一行,并自动附加新元素,如可以使用以下方法快速生成一组平方数列表:

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

输出结果为:

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

4. 列表的切片

4.1 切片

要创建切片,可指定要使用的第一个元素和最后一个元素的索引,与函数range()一样,切片的区间同样是左闭右开的,下面的示例处理的是一个运动队成员列表:

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

此处将原列表的前三个元素切片,输出的仍然是一个列表:

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

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

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

此时输出的是索引为0~3的列表中的元素组成的新列表:

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

要让切片终止于末尾,也可以用类似的语法,不指定后面的索引:

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

则此时输出的为原列表中索引为2及其后面的所有元素组成的新列表:

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

4.2 遍历切片

由于列表的切片也是列表,因此其遍历方法和列表的遍历方法相同,如:

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个元素:

Here are the first three players on my team: 
Charles 
Martina 
Michael

4.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的食品列表,然后创建了一个名为friend_foods的新列表,在不指定任何引的情况下从列表my_foods中提取一个切片,从而创建了这个列表的副本,再将该副本存储到变量friend_foods中。打印每个列表后,发现它们包含的食品相同:

My favorite foods are: 
['pizza', 'falafel', 'carrot cake'] 

My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake']

为核实确实有两个列表,下面在每个列表中都添加一种食品,并核实每个列表都记录了相应人员喜欢的食品:

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

可以看到,原列表的输出与复制后列表的输出结果不同,说明原列表与复制的列表是互相独立的

如果我们只是简单地将my_foods赋给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)

此时的输出结果为:

My favorite foods are: 
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream'] 

My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

可以看到这两个列表是完全相同的,对其中一个列表进行操作也会导致另一个列表发生变化。

5. 使用if语句判断列表是否非空

首先看下面一段代码:

requested_toppings = []

if requested_toppings:
    for requested_topping in requested_toppings: 
        print("Adding " + requested_topping + ".") 
    print("\nFinished making your pizza!") 
else: 
    print("Are you sure you want a plain pizza?")

在这段代码中,首先创建了一个空列表,其中不包含任何元素,然后对其用if语句进行了检查,若列表非空,则会打印出列表中的所有元素,否则返回else处打印的语句:

Are you sure you want a plain pizza?

6. 使用关键字in或not in检查特定元素是否在列表中

要判断特定的值是否已包含在列表中,可使用关键字in:

requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings
'pepperoni' in requested_toppings

此时由于列表中确实含有元素'mushrooms',因此会返回True,而'pepperoni'不在列表元素中,因此会返回False。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋风、萧瑟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值