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]

你可能感兴趣的:(python)