zoukankan      html  css  js  c++  java
  • zip函数的应用

    #!/usr/bin/env python  
    # encoding: utf-8
    from itertools import zip_longest # ➍
    
    # zip并行从输入的各个可迭代对象中获取元素,产
    # 出由 N 个元素组成的元组,只要有一个可迭代
    # 的对象到头了,就默默地停止
    #---------------------------------------------
    #zip_longest
    # 并行从输入的各个可迭代对象中获取元素,产
    # 出由 N 个元素组成的元组,等到最长的可迭代
    # 对象到头后才停止,空缺的值使用  fillvalue
    # 填充
    
    #zip的使用 2个元组变一个字典
    
    
    a = (1, 3)
    b = (2, 4)
    c = dict(zip(a, b))
    print(c)
    
    #字典构建方式
    a = dict(one=1, two=2, three=3)
    b = {'one': 1, 'two': 2, 'three': 3}
    c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
    d = dict([('two', 2), ('one', 1), ('three', 3)])
    e = dict({'three': 3, 'one': 1, 'two': 2})
    print(a == b == c == d == e)
    
    zip(range(3), 'ABC') # ➊
    list(zip(range(3), 'ABC')) # ➋
    list(zip(range(3), 'ABC', [0.0, 1.1, 2.2, 3.3])) # ➌
    list(zip_longest(range(3), 'ABC', [0.0, 1.1, 2.2, 3.3], fillvalue=-1))
    
    
    #❶ zip 函数返回一个生成器,按需生成元组。
    #❷ 为了输出,构建一个列表;通常,我们会迭代生成器。
    #❸ zip 有个奇怪的特性:当一个可迭代对象耗尽后,它不发出警告就停止。
    #❹ itertools.zip_longest 函数的行为有所不同:使用可选的
    #fillvalue(默认值为 None)填充缺失的值,因此可以继续产
    #出,直到最长的可迭代对象耗尽。
    

      

  • 相关阅读:
    Ubuntu下systemd服务的配置
    编译压缩代码 MFCompress-src-1.01 :对‘***’未定义的引用
    德尔福 基础
    德尔福 XE5 安卓权限设置
    德尔福 XE5 安卓调试
    复制任意文件或文件夹到剪贴板
    无法完成安装:'Cannot access storage file '/
    Centos7.4安装kvm虚拟机(使用virt-manager管理)
    MSYS2 使用
    线程
  • 原文地址:https://www.cnblogs.com/c-x-a/p/9186544.html
Copyright © 2011-2022 走看看