Python之列表推导式List comprehensions例解

Python supports a concept called "list comprehensions". It can be used to construct lists in a very natural, easy way, like a mathematician is used to do.

常见python编程方法

#the first try
#
=============================
number = range(10)
size
= len(number)
event
= []
i
= 0
while i < size:
if i%2 == 0:
event.append(i)
i
+= 1
print event

By using Python List comprehensions as below:

#improve
#
=============================
print [i for i in range(10) if i%2 == 0]
Work for fun,Live for love!
原文地址:https://www.cnblogs.com/allenblogs/p/2023593.html