【操作列表】

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

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

这期的分享比较基础,希望可以帮到刚入门的朋友。