[Python] Understand List Comprehensions in Python

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

以上是关于[Python] Understand List Comprehensions in Python的主要内容,如果未能解决你的问题,请参考以下文章

Understand images & containers

understand的安装

Utility3:Understand Dashboard Report

I don’t understand what you mean. 还是 I don’t understand what your means. 哪个对啊

Understand the data

understand EntityManager.joinTransaction()