标签:
解包、封包
解包:把可迭代队形的元素赋值给变量
封包:是把变量构建成元组
x, y =(1, 3)
x, y =[1, 3]
#*mid 表示一个容器,可以包含任意多个元素,解包过程中只能有一个*,*赋值出来的都是列表
>>> head, *tail = list(range(10))
>>> head
0
>>> tail
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> head, *mid ,tail = list(range(10))
>>> head
0
>>> mid
[1, 2, 3, 4, 5, 6, 7, 8]
>>> tail
9
>>>
#取头和尾,_表示将变量丢弃
>>> h, *_, t = list(range(10))
>>> h
0
>>> t
9
>>>
>>> a, (b, c), d = [1 ,[2, 3], 4]
>>>
>>> a
1
>>> b
2
>>> c
3
>>> d
4
>>>
>>> a, (b, *_, c), d = [1, [2, 3, (4, 5, 6), 7, 8, 9], 10]
>>> a
1
>>> b
2
>>> c
9
>>> d
10
>>>
标签:
原文地址:http://www.cnblogs.com/LouisZJ/p/5672987.html