zoukankan      html  css  js  c++  java
  • 【python】内置函数zip

    接收两个或多个序列,并返回一个元组列表,并且每个元组列表包含来自每个序列中的第一个元素

    s = "abc"
    t = [11,23,45]
    print(zip(s,t))
    for pair in zip(s,t):
        print(pair)
    
    print(list(zip(s,t)))

    D:PythonPython35python.exe D:/PycharmProjects/helloworld/src/zipTest.py
    <zip object at 0x00000201D6459648>
    ('a', 11)
    ('b', 23)
    ('c', 45)
    [('a', 11), ('b', 23), ('c', 45)]

    Process finished with exit code 0

    zip的序列长度不同,则取短的那个。

    数量不相同:t长度大于s

    s = "abc"
    t = [11,23,45,77,88]
    print(zip(s,t))
    for pair in zip(s,t):
        print(pair)
    
    print(list(zip(s,t)))
    
    D:PythonPython35python.exe D:/PycharmProjects/helloworld/src/zipTest.py
    <zip object at 0x00000252DE5B96C8>
    ('a', 11)
    ('b', 23)
    ('c', 45)
    [('a', 11), ('b', 23), ('c', 45)]
    
    Process finished with exit code 0

    数量不相同,s长度大于t

    s = "abcer"
    t = [11,23,45]
    print(zip(s,t))
    for pair in zip(s,t):
        print(pair)
    
    print(list(zip(s,t)))
    
    D:PythonPython35python.exe D:/PycharmProjects/helloworld/src/zipTest.py
    <zip object at 0x0000024C7E2C9648>
    ('a', 11)
    ('b', 23)
    ('c', 45)
    [('a', 11), ('b', 23), ('c', 45)]
    
    Process finished with exit code 0
  • 相关阅读:
    luogu2253 好一个一中腰鼓!
    luogu2948 滑雪课
    luogu1556 幸福的路
    luogu1900 自我数
    luogu1632 点的移动
    luogu1999 高维正方体
    树状数组模板
    杜教筛
    [比赛|考试] 9月第一周的考试
    历年NOIP真题总结
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/6691184.html
Copyright © 2011-2022 走看看