zoukankan      html  css  js  c++  java
  • [Python] Understand List Comprehensions in Python

    List comprehensions provide a concise way to create new lists, where each item is the result of an operation applied to each member of an existing list, dictionary or other iterable. Learn how to create your own list comprehensions in this lesson.

    sales = [3.14, 7.99, 10.99, 0.99, 1.24]
    
    sales = [sale * 1.07 for sale in sales]

    With condiion:

    zoo_animals = ['giraffe', 'monkey', 'elephant', 'lion', 'bear', 'pig', 'horse', 'aardvark']
    my_animals = ['monkey', 'bear', 'pig']
    
    other_animals = [animal for animal in zoo_animals if animal not in my_animals]

    Recommended way to do:

    other_animals = [
     animal
     for animal in zoo_animals
     if animal not in other_animals
    ]

    Break down to multi lines make things looks more clear

  • 相关阅读:
    汇编之EBP的认识。
    【转】PE详解
    迟到的,2016年终总结
    Java 反射
    Java 集合与容器类
    Java 类加载与实例化
    Java 类与对象
    Java 值传递与对象拷贝
    Java 面向对象
    二叉树
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8018759.html
Copyright © 2011-2022 走看看