zip函数:

描述:将zip函数中的两个可迭代对象参数按对应索引值进行匹配组合,得到zip对象。(拉链式函数)

zip函数简单应用如下:
#-----------------zip函数-----------------
#第一种zip参数两个可迭代对象元素个数相同
list1=["a","b","c","d","e"]
list2=[1,2,3,4,5]
res1=list(zip(list1,list2))
print(res1)
#输出结果:[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
#第二种zip参数两个可迭代对象元素个数前者较多
list3=["a","b","c","d","e"]
list4=[1,2,3]
res2=list(zip(list3,list4))
print(res2)
#输出结果:[('a', 1), ('b', 2), ('c', 3)]
#第三种zip参数两个可迭代对象元素个数后者较多
list5=["a","b","c"]
list6=[1,2,3,4,5]
res3=list(zip(list5,list6))
print(res3)
#输出结果:[('a', 1), ('b', 2), ('c', 3)]
#第四种zip参数两个可迭代对象均为字符串
list7="hello"
list8=""
res4=list(zip(list7,list8))
print(res4)
#输出结果:[('h', '1'), ('e', '2'), ('l', '3'), ('l', '4'), ('o', '5')]
#第五种zip参数为字典的键值集合
dic1={"name":"kelvin","gender":"man","age":21}
res5=list(zip(dic1.keys(),dic1.values()))
print(res5)
#输出结果:[('h', '1'), ('e', '2'), ('l', '3'), ('l', '4'), ('o', '5')]
#追逐式zip输出
l = ['a', 'b', 'c', 'd', 'e','f']
print(l)
#打印列表
print(list(zip(l[:-1],l[1:])))
#输出结果:
#['a', 'b', 'c', 'd', 'e', 'f']
#[('a', 'b'), ('b', 'c'), ('c', 'd'), ('d', 'e'), ('e', 'f')]