遍历整个列表
使用for循环
>>> magician = ['alice','david','carolina']
>>> for m in magician:
... print(m)
...
alice
david
carolina
创建数值列表
使用函数range()让你能够轻松地生成一系列数。
>>> for value in range(1,5):
... print(value)
...
1
2
3
4
在这个示例中,range()只打印数1-4。这是编程语言中常见的差一行为的结果。函数range()让Python从指定的第一个值开始数,并在到达你指定的第二个值时停止。因为它在第二个值处停止,所以输出不包含该值(这里为5)。
在创建数字列表,可使用函数list()将range()的结果直接转换为列表。
>>> numbers = list(range(1,6))
>>> print(numbers)
[1, 2, 3, 4, 5]
使用函数range()时,还可指定步长。为此,可给这个函数指定第三个参数,Python将根据这个步长来生成数。
>>> even_numbers = list(range(2,11,2))
>>> print(even_numbers)
[2, 4, 6, 8, 10]
使用函数range()几乎能够创建任何需要的数集。例如,如何创建一个列表,其中包含前10个整数(1,10)的平方呢?在Python中,用两个星号(**)表示乘方运算。
>>> for value in range(1,11):
... square = value**2
... squares.append(square)
...
>>> print(squares)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
还可以用列表解析
>>> squares = [value**2 for value in range(1,11)]
>>> 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
一些练习:
1、创建一个列表,其中包含3-30能被3整除的数,再使用一个for循环将这个列表中的数打印出来。
>>> san = list(range(3,30,3))
>>> for s in san:
... print(s)
...
3
6
9
12
15
18
21
24
27
2、将同一个数乘三次称为立方。例如,在Python中,2的立方用2**3表示。请创建一个列表,其中包含前10个整数(1-10)的立方,再使用一个for循环将这些立方数打印出来。
>>> lifang = []
>>> for li in range(1,10):
... lifang.append(li**3)
...
>>> for l in lifang:
... print(l)
...
1
8
27
64
125
216
343
512
729
3、使用列表解析生成一个列表,其中包含前10个整数的立方。
>>> list = [value **3 for value in range(1,11)]
>>> print(list)
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
切片
>>> players = ['charles','martina','michael','florence','eli']
>>> print(players[0:3])
['charles', 'martina', 'michael']
>>> print(players[1:4])
['martina', 'michael', 'florence']
如果没有指定第一个索引,Python将自动从列表开头开始:
>>> print(players[:4])
['charles', 'martina', 'michael', 'florence']
要让切片终止于列表末尾,也可使用类似的语法
>>> print(players[2:])
['michael', 'florence', 'eli']
遍历切片
>>> for play in players[:3]:
... print(play.title())
...
Charles
Martina
Michael
复制列表
>>> my_foods = ['pizza','falafel','carrot cake']
>>> friend_foods = my_foods[:]
>>> print(my_foods)
['pizza', 'falafel', 'carrot cake']
>>> print(friend_foods)
['pizza', 'falafel', 'carrot cake']
为核实确实有两个列表,下面在每个列表中都添加一种食品,并核实每个列表都记录了相应人员喜欢的食品。
>>> my_foods.append('cannoli')
>>> friend_foods.append('ice cream')
>>> print(my_foods)
['pizza', 'falafel', 'carrot cake', 'cannoli']
>>> print(friend_foods)
['pizza', 'falafel', 'carrot cake', 'ice cream']
上面演示了在不使用切片的情况下复制列表的情况。
>>> my_foods=['pizza','falafel','carrot cake']
>>> friend_foods = my_foods
>>> print(my_foods)
['pizza', 'falafel', 'carrot cake']
>>> print(friend_foods)
['pizza', 'falafel', 'carrot cake']
>>> my_foods.append('cannoli')
>>> friend_foods.append('ice cream')
>>> print(my_foods)
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
>>> print(friend_foods)
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']
输出表明,两个列表是相同的,这并非我们想要的结果
friend_foods = my_foods 这种语法实际上是让Python将新变量friend_foods 关联到已与my_foods 相关联的列表,因此这两个变量指向同一个列表。
元组
列表非常适合用于存储在程序运行期间可能变化的数据集。列表是可以修改的,这对处理网站的用户列表或游戏中的角色列表至关重要。然而,有时候你需要创建一系列不可修改的元素,元组可以满足这种需求。Python将不能修改的值称为不可变,而不可变的列表成为元组。
>>> dimensions = (200,50)
>>> print(dimensions[0])
200
>>> print(dimensions[1])
50
尝试修改元组dimensions的一个元素,看看结果如何
>>> dimensions[0] = 250
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
注意:严格地说,元组是由逗号标识的,圆括号只是让元组看起来更整洁、更清晰。如果你要定义只包含一个元素的元组,必须在这个元素后面加上逗号。
>>> my_t = (3,)
创建只包含一个元素的元组通常没有意义,但自动生成的元组有可能只有一个元素。
遍历元组中的所有值
像列表一样,也可以使用for循环来遍历元组中的所有值。
>>> for dimension in dimensions:
... print(dimension)
...
200
50
修改元组变量
>>> dimensions = (400,100)
>>> for dimension in dimensions:
... print(dimension)
...
400
100
给元组变量重新赋值是合法的。