LoginSignup
0
1

More than 5 years have passed since last update.

Python > Comprehension / 内包表記 > List comprehension

Last updated at Posted at 2017-02-22

@ 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文を続けることもできる。

0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1