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