zoukankan      html  css  js  c++  java
  • python comprehension

    python comprehension

    python提供了一种语法,让从已存在的集合上创建集合更加容易的办法,英文称为Comprehension.

    List comprehesion are used to map input values to output values, applying a filter along the way to include or exclude any values that meet a specific condition.

    Example:

    input_strings = ["1", "5", "28", "131", "3"]
    # without comprehension
    output_integers = []
    for num in input_strings:
        output_integers.append(int(num))
    print(output_integers)
    
    
    # with comprehension
    output_integers = [int(num) for num in input_strings]
    print(output_integers)
    
    # with filter
    output_integers = [int(num) for num in input_strings if len(num) < 3]
    print(output_integers)
    
    

    输出:

    [1, 5, 28, 131, 3]
    [1, 5, 28, 131, 3]
    [1, 5, 28, 3]
    
  • 相关阅读:
    JavaScript之正则表达式
    BOM之本地数据存储
    BOM之定时器
    BOM之window核心模块
    BOM简介
    DOM之元素定位
    DOM之事件
    DOM之节点操作
    DOM简介
    linux机制
  • 原文地址:https://www.cnblogs.com/buzhouke/p/14258591.html
Copyright © 2011-2022 走看看