Life is short, you need Python

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]
posted @ 2011-04-21 14:45  runfox545  阅读(1527)  评论(0编辑  收藏  举报
白月黑羽 Python教程 白月黑羽Python