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
  • 相关阅读:
    winform 与 html 交互 简单案例
    Winform窗口弹出位置控制
    c#预处理指令
    最简单的数据绑定
    Asp.Net细节性问题精萃
    AjaxUploader使用
    Log4net对文件的支持
    Log4net对数据库的支持
    Log4net使用
    Log4net介绍
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/6691184.html
Copyright © 2011-2022 走看看