@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 2285 / 12833)
Comprehension make it possible for you to combine loops and conditional tests with a less verbose syntax.
The simplest form
@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 2319 / 12833)
The simplest form of list comprehension is:
[ expression for item in iterable ]
例を参考に実装してみた。
http://ideone.com/9l9LpH
number_list = [ 2 * number - 1 for number in range(1,5)]
print(number_list)
[1, 3, 5, 7]
include a conditional expression
@ Introducing Python: Modern Computing in Simple Packages by Bill Lubanovic
(No. 2319 / 12833)
[ expression for item in iterable if condition ]
number_list = [ number for number in range(1,9) if number % 2 == 1]
print(number_list)
[1, 3, 5, 7]
日本語訳
内包表記のことのようですね。
http://qiita.com/7of9/items/d03d099b400d9a067086#comment-a3d81d6be1e59c4a0a35
http://qiita.com/taiga_6404/items/20a3d9a7edf1f7bd6a2c
http://qiita.com/supersaiakujin/items/0776d3252c5000ca146e
for文のあとにfor文を続けることもできる。