一般情况下

x,y,z = 1,2,3
print("x:",x)
print("y:",y)
print("z:",z)
#运行结果
x: 1
y: 2
z: 3

对元祖序列解包

name = ("qiaobushi","wanglihong","leibushi")
x,y,z = name
print(name)
print("x:",x)
print("y:",y)
print("z:",z)
#运行结果
('qiaobushi', 'wanglihong', 'leibushi')
x: qiaobushi
y: wanglihong
z: leibushi

对列表序列解包

str = "123,456,789"
str = str.split(",")
print(str)
x,y,z = str
print("x:",x)
print("y:",y)
print("z:",z)
#运行结果
['123', '456', '789']
x: 123
y: 456
z: 789

对dict序列解包获得key和value

#例1
dict1 = {"one":1,"two":2,"three":3}
x,y,z = dict1 
print("key-x:",x)
print("key-y:",y)
print("key-z:",z)
print("value-x:",dict1[x])
print("value-y:",dict1[y])
print("value-z:",dict1[z])

#例2
dict1 = {"one":1}
x,y = dict1.popitem()
print("key:",x,"value:",y)
#运行结果
key-x: one
key-y: two
key-z: three
value-x: 1
value-y: 2
value-z: 3
key: one value: 1

以上这篇python-序列解包(对可迭代元素的快速取值方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com