@ 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 ]
I implemented it with reference to the example. http://ideone.com/9l9LpH
number_list = [ 2 * number - 1 for number in range(1,5)]
print(number_list)
result
[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 ]
http://ideone.com/A1AkaE
number_list = [ number for number in range(1,9) if number % 2 == 1]
print(number_list)
result
[1, 3, 5, 7]
It sounds like a comprehension.
http://qiita.com/7of9/items/d03d099b400d9a067086#comment-a3d81d6be1e59c4a0a35 http://qiita.com/taiga_6404/items/20a3d9a7edf1f7bd6a2c http://qiita.com/supersaiakujin/items/0776d3252c5000ca146e
You can also follow the for statement with the for statement.
Recommended Posts