One - One Code All

Blog Content

python基础内置函数之zip

Python   2012-03-04 15:04:22
  1. 介绍:zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表。如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以将元组解压为列表。

  2. 语法:zip([iterable, ...]) ,iterabl -- 一个或多个迭代器。

  3. 举例:

    a = [1,2,3]
    b = [4,5,6]
    c = [4,5,6,7,8]
    zipped = zip(a,b)
    for i in zipped:
        print(i)
    
    for (x,y) in zip(a,b):
        print(x,y,'--',x*y)
    
    abc = list(zip(a, b, c))
    d = list(zip(*abc))
    print(list(zip(*abc)))
    print(d)
    
    for x,y,z in d:
        print(x,end=":")
        print(y)


上一篇:python基础内置函数之print
下一篇:python list求index便捷方法

The minute you think of giving up, think of the reason why you held on so long.