先看一个简单的例子:
student1 = ("David", 90)
stuName, stuScore = student1;
print('Name: %s, Score: %s' %(stuName, stuScore))
其中第二行代码就是一个简单的元组拆包,它将一个学生对象按照Name, Score拆分,并赋值给stuName, stuScore这两个变量,这种简单的元组拆包叫做平行赋值
还可以用*将一个可迭代对象拆分成函数的参数:
t = [88,20]
print(*t)
print(divmod(*t))
产生的结果是:
88 20
(4, 8)
除此之外,可以用*来处理剩下的元素:
a,b,*rest = range(10)
print(a,b,rest)
这时a = 0; b = 1; rest = [2,3,4,5,6,7,8,9]
a,*rest,b = range(10)
print(a,rest,b)
这时a = 0; rest = [1,2,3,4,5,6,7,8]; b = 9
a, b, *rest = range(3)
print(a,b,rest)
这时a = 0; b = 1; rest = [2]
a, b, *rest = range(2)
print(a,b,rest)
这时a = 0; b = 1; rest = []
但是不用*来表示剩余元素时,编译过程就会报错:
a, b, rest = range(10)
print(a,b,rest)
Traceback (most recent call last):
File "D:/PythonWorkSpace/FluentPython/c2/c2-7.py", line 21, in <module>
a, b, rest = range(10)
ValueError: too many values to unpack (expected 3)
总结一下,元组拆包可以应用到任何可迭代的对象上,唯一的硬性要求是,被拆包的对象的元素数量必须和接受这些元素的元组的空档数一致,除非用*来表示忽略多余的元素。
嵌套元组拆包
students = [
('David', 'male', (100, 90)),
('Nancy', 'female', (95,88)),
('Joe', 'male', (80,95)),
('Lucy', 'female', (100,83)),
]
print('{:6} | {:^6} | {:^6}'.format('', 'score1', 'score2'))
fmt = '{:6} | {:6} | {:6}'
for name, sex,(score1, score2) in students:
if score1 + score2 >= 180:
print(fmt.format(name, score1, score2))
students列表是嵌套式的,分别代表姓名,性别,成绩1,成绩2,最后输出:
| score1 | score2
David | 100 | 90
Nancy | 95 | 88
Lucy | 100 | 83