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

时间:2021-05-22

一般情况下

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

对元祖序列解包

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

对列表序列解包

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

对dict序列解包获得key和value

#例1dict1 = {"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])#例2dict1 = {"one":1}x,y = dict1.popitem()print("key:",x,"value:",y)#运行结果key-x: onekey-y: twokey-z: threevalue-x: 1value-y: 2value-z: 3key: one value: 1

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章