zoukankan      html  css  js  c++  java
  • Python3-笔记-C-002-函数-zip

    def testzip():
    # zip是将每个可迭代对象的对应位置元素打包成一个tuple
    # 操作符*zip函数配合可以实现与zip相反的功能, 即将合并的序列拆成多个tuple
    # 新的序列的长度以参数中最短的序列为准
    x = [1, 2, 3]
    y = [4, 5, 6]
    z = [7, 8, 9]
    xyz = list(zip(x, y, z)) # <class 'list'>: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    x = [1, 2, 3]
    y = [4, 5, 6, 7]
    xy = list(zip(x, y)) # <class 'list'>: [(1, 4), (2, 5), (3, 6)]
    x = [1, 2, 3]
    x = list(zip(x)) # <class 'list'>: [(1,), (2,), (3,)]
    x = list(zip()) # <class 'list'>: []
    x = [1, 2, 3]
    y = [4, 5, 6]
    z = [7, 8, 9]
    xyz = list(zip(x, y, z)) # <class 'list'>: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
    u = list(zip(*xyz)) # <class 'list'>: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
    x = [1, 2, 3]
    r = list(zip(*[x] * 3)) # <class 'list'>: [(1, 1, 1), (2, 2, 2), (3, 3, 3)]

    # 使用zip反转字典
    m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
    m2 = dict(zip(m.values(), m.keys()))
  • 相关阅读:
    简单记录下springboot+jms+activemq
    简单记录下RestTemplate 中postForObject调用例子
    vue+springboot后台实现页面按钮权限
    发送短信功能
    drf
    drf
    drf
    drf
    drf
    drf
  • 原文地址:https://www.cnblogs.com/vito13/p/7730019.html
Copyright © 2011-2022 走看看