アットウィキロゴ

Python

List comprehensions

List comprehensions provides an easy way of creating a list. You should not use the functions of filter or map.
>>> aList = [i for i in xrange(10)]
>>> print aList
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> aList = [i for i in xrange(10) if i % 2 == 0]
>>> print aList
[0, 2, 4, 6, 8]
>>> aList = [i**2 for i in aList]
>>> print aList
[0, 4, 16, 36, 64]
 
最終更新:2011年07月03日 20:28