您的位置:首页 > 编程语言 > Python开发

深入解析Python小白学习【操作列表】

2019-03-28 11:04 781 查看

1.遍历列表

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

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

 循环中,Python将首先读取其中的第一行代码:

for magician in magicians:

这行代码让Python获取列表magicians 中的第一个值('alice' ),并将其存储到变量magician 中。接下来,Python读取下一行代码:

print(magician)

它让Python打印magician 的值――依然是'alice' 。鉴于该列表还包含其他值,Python返回到循环的第一行:

for magician in magicians:

Python获取列表中的下一个名字――'david' ,并将其存储到变量magician 中,再执行下面这行代码: 

print(magician)

以此类推,直至列表的最后一个元素。

对列表中的每个元素,都将执行循环指定的步骤,而不管列表包含多少个元素。如果列表包含一百万个元素,Python就重复执行指定的步骤一百万次,且通常速度非常快。 使用for 循环处理数据是一种对数据集执行整体操作的不错的方式。

2.避免缩进错误,Python根据缩进来判断代码行与前一个代码行的关系

2.1未缩进:

magicians = ['alice','david','carolina']for magician in magicians:print(magician)
IndentationError: expected an indented block

2.2循环后的冒号

 for 语句末尾的冒号告诉Python,下一行是循环的第一行。如果你不小心遗漏了冒号,将导致语法错误。

3.创建数值列表

3.1函数range()

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

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

3.2使用range()创建数字列表

将range() 作为list() 的参数,输出将为一个数字列表。

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

range()函数还可指定步长:

even_numbers = list(range(1,13,2))
print(even_numbers)
>>>[1, 3, 5, 7, 9, 11]

函数range() 从1开始数,然后不断地加2,直到达到或超过终值。

使用函数range() 几乎能够创建任何需要的数字集。

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

4.列表解析

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

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

首先,指定一个描述性的列表名,如squares。然后指定一个左方括号,并定义一个表达式,用于生成你要存储到列表中的值。在这个示例中,表达式为value**2 ,它计算平方值。接下来,编写一个for 循环,用于给表达式提供值,再加上右方括号。在这个示例中,for 循环为for value in range(1,11) ,它将值1~10提供给表达式value**2 。请注意,这里的for 语句末尾没有冒号。

5.列表切片(处理部分列表元素)

与range()一样,指定要使用的第一个元素和最后一个元素的索引,到达指定的第二个索引值前面的元素后停止。

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

未指定起始索引及终止索引的情况: 

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

6.遍历切片

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

players = ['charles','martina','michael','florence','eli']
print("Here are the first three players in my team:")
for player in players[0:3]:
print(player.title())
>>>Here are the first three players in my team:
>>>Charles
>>>Martina
>>>Michael

处理数据时,可使用切片来进行批量处理;编写Web应用程序时,可使用切片来分页显示信息。

7.复制列表

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

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 = ['pizza','falafel','carrot cake']
# friend_foods和my_foods指向同一个列表
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']

8.元组

列表是可以修改的,然而,需要创建一系列不可修改的元素,元组可以满足这种需求。不可变的列表被称为元组 。

元组看起来犹如列表,但使用圆括号而不是方括号来标识。

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

元组元素不可更改: 

dimensions = (200,50)
dimensions[0] = 230

>>>dimensions[0] = 230
>>>TypeError: 'tuple' object does not support item assignment

8.1 for 循环遍历元组

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

8.2修改元组变量

元组元素不可更改,但可给存储元组的变量赋值。

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

dimensions = (50,40,30)
for dimension in dimensions:
print(dimension)
>>>200
>>>50
>>>100
>>>50
>>>40
>>>30

相比于列表,元组是更简单的数据结构。如果需要存储的一组值在程序的整个生命周期内都不变,可使用元组。

以上所述是小编给大家介绍的Python操作列表详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: