列表2-python基础篇五

遍历整个列表

  通过使用for 循环遍历列表:
magicians = ['alice', 'david', 'carolina']
for magician in magicians: 
  print(magician)
"""
alice
david
carolina
"""

 创建数值列表

  1、Python函数range() 让你能够轻松地生成一系列的数字。
  函数range() 让Python从你指定的第一个值开始数,并在到达你指定的第二个值后停止,因此输出不包含第二个值(这里为5)。
for value in range(1,5):
  print(value)
'''
1
2
3
4
'''

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

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

#函数range() 从2开始数,然后不断地加2,直到达到或超过终值(11)
even_numbers = list(range(2,11,2))
print(even_numbers)
#[2, 4, 6, 8, 10]

#创建一个列表,其中包含前10个整数(即1~10)的平方
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]

  几个简单的统计数字的函数

>>> digits = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> min(digits)  #最小值
0
>>> max(digits)  #最大值
9
>>> sum(digits)  #总和
45
  编写一行代码就能生成列表,在这个示例中,for 循环为for value in range(1,11) ,它将值1~10提供给表达式value**2 。请注意,这里的for 语句末尾没有冒号。
squares = [value**2 for value in range(1,11)]
print(squares)
#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

 

列表的切片

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

  要创建切片,可指定要使用的第一个元素和最后一个元素的索引。与函数range() 一样,Python在到达你指定的第二个索引前面的元素后停止。要输出列表中的前三个元素,需要指定索引0~3,这将输出分别为0 、1 和2 的元素。

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

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

#如果你没有指定第一个索引,Python将自动从列表开头开始
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
#['charles', 'martina', 'michael', 'florence']

#Python将返回从第3个元素到列表末尾的所有元素
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])
#['michael', 'florence', 'eli']

#如果你要输出名单上的最后三名队员,可使用切片players[-3:]
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])

   遍历切片的方法

players = ['charles', 'martina', 'michael', '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
 Michael
'''

 复制列表

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

  这里将my_foods 赋给friend_foods ,而不是将my_foods 的副本存储到friend_foods 。这种语法实际上是让Python将新变量friend_foods 关联到包含在my_foods 中的列表,因此这两个变量都指向同一个列表。鉴于此,当我们将'cannoli' 添加到my_foods 中时,它也将出现在friend_foods 中;同样,虽然'icecream' 好像只被加入到了friend_foods 中,但它也将出现在这两个列表中。

元祖

  Python将不能修改的值称为不可变的 不 ,而不可变的列表被称为元组元 。

  元组看起来犹如列表,但使用圆括号而不是方括号来标识。定义元组后,就可以使用索引来访问其元素,就像访问列表元素一样。

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

#尝试修改会报错
dimensions = (200, 50)
dimensions[0] = 250
'''
Traceback (most recent call last): File "dimensions.py", line 3, in <module> dimensions[0] = 250 TypeError: 'tuple' object does not support item assignment
'''

  遍历元组中的所有值

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

  修改元组变量。虽然不能修改元组的元素,但可以给存储元组的变量赋值。因此,如果要修改前述矩形的尺寸,可重新定义整个元组:

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

重新定义元组
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
    print(dimension)

 

posted @ 2021-09-03 15:50  逍遥abin  阅读(44)  评论(0编辑  收藏  举报