Python解壓序列

一.普遍情況:

``````x,y,z = 1,2,3

print("x:",x)
# x:1
print("y:",y)
# y:2
print("z:",z)
# 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
'''
``````

三.針對字符串:

``````a,b,c='hel'
print(a)
print(b)
print(c)
# h
# e
# l
``````

四.針對列表:

``````# 定義績效列表
list = [10,3,5,6,22,8,12,44]

# 需求：只要開頭和結尾的數值
# *代表中間所有的  _代表一個變量，因爲我們不想要了，所以使用_，*後面必須跟一個變量
a,*_,c=list
print(a)
print(c)
# 10
# 44

a,*d,c=list
print(a)
# 10
print(d)
# [3, 5, 6, 22, 8, 12]
print(c)
# 44
``````

五.針對dict序列解包獲:得key和value

``````#學習中遇到問題沒人解答？小編創建了一個Python學習交流羣：711312441
#法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
'''
``````

六.對變量的值進行交換:

``````f1=1
f2=2

# 需求把f1和f2的值進行交換
#
f1,f2=f2,f1
print(f1,f2)
# 2 1
``````