Python元组拆包赋值

元组拆包赋值

>>> 1,2,4   #相当于一个元组
(1, 2, 4)

>>> a,b,c = 1,2,4
>>> a,b,c = (1,2,4)
>>> a,b,c
(1, 2, 4)


>>> for country,_ in traveler_ids:
	print(country)


>>> traveler_ids = [('USA','31195855'),('BRA','CE3425')]
>>> for passport in sorted(traveler_ids):
	print('%s/%s' % passport)

	
BRA/CE3425
USA/31195855

* 运算符表示个元组

(1, 2, 4)
>>> t = (1,2)
>>> def add(x,y):
	return x+y

>>> add(*t)  #元组拆包赋值
3
>>> 

>>> a,b, *rest = range(5)
>>> a,b,rest
(0, 1, [2, 3, 4])
>>> a,b,*rest = range(3)
>>> a,b,rest
(0, 1, [2])
>>> a,b,*rest = range(2)
>>> a,b,rest
(0, 1, [])
>>> 

你可能感兴趣的:(python)