一、序列解包
Python赋值语句技巧之序列解包(sequence unpacking),是Python语言赋值语句的一种技巧方法,在给多个Python变量命名同时赋值时是很有效率的一种方法。简单说就是通过一条命令把string/list/tuple的元素分别赋值给多个变量。
# string解包;
>>> s = 'Hello'
>>> a, b, c, d, e = s
>>> print(a, b, c, d, e)
H e l l o
# list解包;
>>> user = ['dkey', 22, 170]
>>> name, age, height = user
>>> print(name, age, height)
dkey 22 170
# tuple解包;
>>> user = ('dkey', 22, 170)
>>> name, age, height = user
>>> print(name, age, height)
dkey 22 170
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# string解包;
>>>s='Hello'
>>>a,b,c,d,e=s
>>>print(a,b,c,d,e)
Hello
# list解包;
>>>user=['dkey',22,170]
>>>name,age,height=user
>>>print(name,age,height)
dkey22170
# tuple解包;
>>>user=('dkey',22,170)
>>>name,age,height=user
>>>print(name,age,height)
dkey22170
这种解包有什么好处呢?可以通过这种拆包把string/list/tuple的数据它的一个位置给它对应出一个属性。可以避免通过索引多次赋值。
解包在循环语句中的使用方法:
>>> user = [('a', 1, 170), ('b', 2, 175)]
>>> for name, age, height in user:
... print(name, age, height)
...
a 1 170
b 2 175
>>> for name, *other in user:
... print(name, *other)
...
a 1 170
b 2 175
1
2
3
4
5
6
7
8
9
10
11
12
>>>user=[('a',1,170),('b',2,175)]
>>>forname,age,heightinuser:
...print(name,age,height)
...
a1170
b2175
>>>forname,*otherinuser:
...print(name,*other)
...
a1170
b2175
不光如此,解包对于获取字典的key和value非常有用,如下:
>>> user = {'name':'dkey', 'age':'25'}
>>> for k, v in user.items():
... print(k, v)
...
age 25
name dkey
1
2
3
4
5
6
>>>user={'name':'dkey','age':'25'}
>>>fork,vinuser.items():
...print(k,v)
...
age25
namedkey
最后,再说一种解包指定元素的方法:
>>> user = ['dkey', 22, 170, 'shanghai']
>>> _, age, _, _ = user
>>> age
22
1
2
3
4
>>>user=['dkey',22,170,'shanghai']
>>>_,age,_,_=user
>>>age
22
二、参数解包
解包除了这个简单应用外,也有一些复杂应用方式,比如使用“*”,“**”进行参数解包。这是Python 3.5添加的新特性(PEP 448),使得*a、**d可以在函数参数之外使用:
>>> print(*range(5))
0 1 2 3 4
>>> lst = [0, 1, 2, 3]
>>> print(*lst)
0 1 2 3
>>> a = *range(3), #这里的逗号不能漏掉
>>> print(a)
(0, 1, 2)
>>> d = {"hello": "world", "python": "rocks"}
>>> print({**d}["python"])
rocks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>>print(*range(5))
01234
>>>lst=[0,1,2,3]
>>>print(*lst)
0123
>>>a=*range(3),#这里的逗号不能漏掉
>>>print(a)
(0,1,2)
>>>d={"hello":"world","python":"rocks"}
>>>print({**d}["python"])
rocks
哪里会应用呢?比如说对于user元组,我只想提取它其中的name属性,后面的都不需要,我们可以这么做。
>>> user = ('dkey', 22, 170)
>>> name, *other = user
>>> print(name, *other) #带*号的
dkey 22 170
>>> print(name, other) #不带*号的
dkey [22, 170]
1
2
3
4
5
6
>>>user=('dkey',22,170)
>>>name,*other=user
>>>print(name,*other)#带*号的
dkey22170
>>>print(name,other)#不带*号的
dkey[22,170]
这种“*other”的用法可以在前-中-后任何位置使用。这么做的好处就是,可以不用关心这个list/tuple的元素增多或减少而抛异常,因为按照上面那种使用方法属性跟元素必须一一对应,多或少都会抛异常。
参数解包在函数中使用的比较多,如下:
>>> list = [1,2,3]
>>> def add_fn(a, b, c):
... return a + b + c
...
>>> print(add_fn(*list))
6
1
2
3
4
5
6
>>>list=[1,2,3]
>>>defadd_fn(a,b,c):
...returna+b+c
...
>>>print(add_fn(*list))
6
所谓的解包(Unpacking)实际上可以看做是去掉()的元组或者是去掉{}的字典。这一语法也提供了一个更加 Pythonic 地合并字典的方法:
>>> user = {'name': "Trey", 'website': "http://treyhunner.com"}
>>> defaults = {'name': "Anonymous User", 'page_name': "Profile Page"}
>>> print({**defaults, **user})
{'name': 'Trey', 'page_name': 'Profile Page', 'website': 'http://treyhunner.com'}
1
2
3
4
>>>user={'name':"Trey",'website':"http://treyhunner.com"}
>>>defaults={'name':"Anonymous User",'page_name':"Profile Page"}
>>>print({**defaults,**user})
{'name':'Trey','page_name':'Profile Page','website':'http://treyhunner.com'}
如果您觉得本站对你有帮助,那么可以支付宝扫码捐助以帮助本站更好地发展,在此谢过。