首页 > 编程语言 > 详细

[Python] Understand List Comprehensions in Python

时间:2017-12-10 23:46:18      阅读:199      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!