python中的zip函数

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# zip函数需要传入迭代器,返回一个是zip对象,也是一个迭代器,内部的元素是元组形式
new_zip = (zip(a, b))
for i in new_zip:
    print(i)
print(new_zip, type(new_zip))

# zip函数也可以将两个迭代器转换为字典
# new_dict = dict(zip(a, b))
new_dict = {k: v for k, v in zip(a, b)}
print(new_dict)
posted @ 2024-09-06 11:18  愿风带走思绪  阅读(15)  评论(0)    收藏  举报