python中zip函数

python中zip函数

1、python中zip函数用于返回由可迭代参数共同组成的元组。

 

长度不一致时,以短的序列进行迭代。

>>> test1 = ["aaa","bbb","ccc","ddd"]
>>> test2 = (111,222,333,444,555)
>>> test3 = "abcde"
>>> for i in zip(test1, test2):
    print(i)

    
('aaa', 111)
('bbb', 222)
('ccc', 333)
('ddd', 444)
>>> for i in zip(test1,test3):
    print(i)

    
('aaa', 'a')
('bbb', 'b')
('ccc', 'c')
('ddd', 'd')
>>> for i in zip(test1,test2,test3):
    print(i)

    
('aaa', 111, 'a')
('bbb', 222, 'b')
('ccc', 333, 'c')
('ddd', 444, 'd')

 

posted @ 2021-05-01 20:44  小鲨鱼2018  阅读(596)  评论(0编辑  收藏  举报