python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python-序列解包(对可迭代元素的快速取值)

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

作者:我要七龙ru

今天小编就为大家分享一篇python-序列解包(对可迭代元素的快速取值方法),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

一般情况下

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

您可能感兴趣的文章:
阅读全文