Skip to Content
強力なPython
book

強力なPython

by Aaron Maxwell
March 2025
Intermediate to advanced
200 pages
2h 51m
Japanese
O'Reilly Media, Inc.
Content preview from 強力なPython

第2章. 包括でコレクションを作成する

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

リスト内包表記は、リストを作成するための高レベルで宣言的な方法である。以下のようになる:

>>> squares = [ n*n for n in range(6) ]
>>> print(squares)
[0, 1, 4, 9, 16, 25]

これは本質的に以下のことと等価性である:

>>> squares = []
>>> for n in range(6):
...     squares.append(n*n)
>>> print(squares)
[0, 1, 4, 9, 16, 25]

最初の例では、あなたが入力するのはどのようなリストが欲しいかを宣言しているのに対して、2番目はそれをどのように作成するかを指定していることに注目してほしい。どのようなリストを作成したいかを宣言し、それをPythonがどのように作成するかを決定する。

Pythonではリスト以外にも内包表記を書くことができる。例えば、簡単な辞書内包を紹介しよう:

>>> blocks = { n: "x" * n for n in range(5) }
>>> print(blocks)
{0: '', 1: 'x', 2: 'xx', 3: 'xxx', 4: 'xxxx'}

これは以下と等価性である:

>>> blocks = dict()
>>> for n in range(5):
...     blocks[n] = "x" * n
>>> print(blocks)
{0: '', 1: 'x', 2: 'xx', 3: 'xxx', 4: 'xxxx'}

内包の主な利点は、読みやすさと保守性である。初めて内包に出会う開発者であっても、その意味について最初に推測したことが正しいことを発見するのが普通だ。これ以上に読みやすいものはない。

また、より深い認知的利点もある。理解力を少し練習すれば、精神的な努力をほとんどせずに書き方の練習ができることに発見するだろう。

リストや辞書以外にも、この章で学ぶ内包表記がいくつかある。 それらに慣れてくると、それらが汎用的で非常にPython的であることが分かるだろう。つまり、それらは他の多くのPythonイディオムや構成要素にうまく適合し、あなたのコードに新たな表現力とエレガンスを与えてくれる。

リスト内包表記

リスト内包表記は、最も広く使われている種類の内包で、基本的にはリストを作成し、それを入力する方法である。 その構造は次のようなものだ:

[ EXPRESSION for VARIABLE in SEQUENCE ]

EXPRESSION はPythonの任意の式であるが、有用な内包では式に変数が含まれていることが多い。その変数は、 フィールドに記述される。 は、変数が列挙するソース値を定義し、最終的な計算値のシーケンスを作成する。VARIABLE SEQUENCE

先程の簡単な例である:

>>> squares = [ n*n for n in range(6) ]
>>> type(squares)
<class 'list'>
>>> print(squares)
[0, 1, 4, 9, 16, 25]

結果は単なる正規リストである。squares において、式はn*n であり、変数はn であり、ソースシーケンスはrange(6)

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AWS上のレジリエントなシステムの構築

AWS上のレジリエントなシステムの構築

Kevin Schwarz, Jennifer Moran, Nate Bachmeier

Publisher Resources

ISBN: 9798341634039