蝙蝠岛资源网 Design By www.hbtsch.com

一般情况下

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-序列解包(对可迭代元素的快速取值方法)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,序列解包

蝙蝠岛资源网 Design By www.hbtsch.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
蝙蝠岛资源网 Design By www.hbtsch.com

评论“python-序列解包(对可迭代元素的快速取值方法)”

暂无python-序列解包(对可迭代元素的快速取值方法)的评论...