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
  • 相关阅读:
    day_15补充加总结
    Day_15 内置模块er
    sort 函数 与 sorted 函数区别
    python 代码风格------------PEP8规则
    python 返回值
    函数记忆 : startswith() 与 endswith
    RE模块使用
    正则表达式
    collections
    函数datetime
  • 原文地址:https://www.cnblogs.com/AlexBai326/p/6691184.html
Copyright © 2011-2022 走看看