python zip dict

一.zip函数:接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表

x = [1,2,3]
y=[4,5,6]
xy = zip(x,y)
print(xy)
py2会[(1, 4), (2, 5), (3, 6)]
py3:<zip object at 0x000001F30A7AAFC8>
py3要想看tuple的元素可以:

for e in xy:
print(e)

(1, 4)
(2, 5)
(3, 6)

 

x = [1,2,3]
y=[4,5,6]
xy = zip(x,y)
u = dict(xy)
print(u)

{1: 4, 2: 5, 3: 6}
keys = ['a','b','c']
values = [1,2,3]
x = zip(keys,values)
y = dict(x)
for k,v in y.items():
    print(k,' ',v)

a 1
b 2
c 3

posted on 2020-03-25 15:04  cltt  阅读(222)  评论(0编辑  收藏  举报

导航