python关于解构的例子

解构

a,b = 11,22
print(a)
print(b)

a = 11
b = 22
a,b = b,a
print(a,b)  #输出 22 11

a,b = [1,2]
print(a,b)   #输出1 2

a,b = (22,88)
print(a,b)

a,_,b = (7,8,9)
print(a,b)  #等号后边的值和前面的要一一对应

a = 10,11 # 本质就是一个元组
print(a)   #输出 (10, 11)

a,b = "26"
print(a,b)  #输出 2 6

a,b = {"1":"a","2":"b"}
print(a,b)  #  输出 1 2

lat = {"1":"123","4":"456","7":"789"}
for i in lat.items():
    print(i)
输出:
('1', '123')
('4', '456')
('7', '789')

lat = {"1":"123","4":"456","7":"789"}

for x,b in lat.items():
    print(x,b)
和
for i in lat.items():
    print(i[0],i[1])  #两种方法实现结果相同

输出 
1 123
4 456
7 789
posted @ 2020-04-06 15:29  Jerry!  阅读(420)  评论(0编辑  收藏  举报