一般情况下
x,y,z = 1,2,3
print("x:",x)
print("y:",y)
print("z:",z)
|
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
|
|