码迷,mamicode.com
首页 > 编程语言 > 详细

[Python] Understand List Comprehensions in Python

时间:2017-12-11 00:40:35      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:sso   ant   body   pig   python   down   mon   res   new   

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

标签:sso   ant   body   pig   python   down   mon   res   new   

原文地址:http://www.cnblogs.com/Answer1215/p/8018759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!