<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「ジェネレータ内包表記」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/%E3%82%B8%E3%82%A7%E3%83%8D%E3%83%AC%E3%83%BC%E3%82%BF%E5%86%85%E5%8C%85%E8%A1%A8%E8%A8%98/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 26 Oct 2025 06:03:53 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「ジェネレータ内包表記」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【Python】ジェネレータ関数とジェネレータの基本</title>
		<link>https://tech.nkhn37.net/python-generator-yield/</link>
					<comments>https://tech.nkhn37.net/python-generator-yield/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Mon, 01 Feb 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[関数]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[yield]]></category>
		<category><![CDATA[ジェネレータ]]></category>
		<category><![CDATA[ジェネレータ内包表記]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=583</guid>

					<description><![CDATA[Pythonのジェネレータ関数とジェネレータの基本について解説します。 ジェネレータ関数とジェネレータ Pythonにおけるジェネレータ関数とは、yield 文を使って値を返却する関数であり、その実行によってジェネレータ [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Pythonの<span class="marker"><strong>ジェネレータ関数とジェネレータの基本</strong></span>について解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="ジェネレータ-generator-関数">ジェネレータ関数とジェネレータ</h2>



<p>Pythonにおける<span class="marker"><strong>ジェネレータ関数</strong></span>とは、<span class="marker"><strong><code>yield</code></strong></span> 文を使って値を返却する関数であり、その実行によって<span class="marker"><strong>ジェネレータ</strong></span>というオブジェクトが作成されます。ジェネレータはイテレータの一種であり、逐次的に値を生成して返すオブジェクトのことを言います。なお、イテレータについては「<a href="https://tech.nkhn37.net/python-iterator-basics/" target="_blank" rel="noreferrer noopener">イテレータの基本</a>」を参考にしてください。</p>



<p>このため、ジェネレータを使うとシーケンス全体をメモリ上に展開せず、無限のシーケンスであっても必要な時に必要な値だけを生成することができ、メモリ使用量を大幅に削減できます。</p>



<p>この記事では、ジェネレータ関数とジェネレータについて基本の使い方を紹介します。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="ジェネレーター関数の定義方法-yieldによる返却">ジェネレータ関数の定義方法</h3>



<p>ジェネレータ関数は、通常の関数とほぼ同じ定義方法ですが、<code>return</code> 文の代わりに <span class="marker"><strong><code>yield</code></strong></span> 文を使用します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># ジェネレータ関数
def generate_char():
    yield "A"
    yield "B"
    yield "C"


if __name__ == "__main__":
    gen = generate_char()
    print(type(gen))

    # Aが出力される
    print(next(gen))
    # Bが出力される
    print(next(gen))
    # Cが出力される
    print(next(gen))</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
&lt;class 'generator'>
A
B
C</pre>



<p>上記の<code>generate_char</code>関数は、<code>"A"</code>、<code>"B"</code>、<code>"C"</code>という文字列を順に返すジェネレータ関数です。<code>gen</code>はジェネレータのオブジェクト（<code>&lt;class 'generator'&gt;</code>）となります。</p>



<p>ジェネレータはイテレータの一種であり、<code>__next__()</code>メソッドが実装されているため、<code>next</code>関数を使って順番に値を取り出せます。</p>



<p>ジェネレータ関数は<code>yield</code>文まで実行され、その位置を記憶します。次に呼び出されると、その位置から実行を再開します。例えば、最初の呼び出しでは<code>yield "A"</code>が実行され、次の呼び出しでは<code>yield "B"</code>が実行されます。</p>



<p><code>yield "C"</code>までが実行され、さらに次の呼び出しで返す値がなくなると、ジェネレータは、<code>StopIteration</code>例外を送出します。</p>



<p>ジェネレータはイテレータの一種のため<code>for</code>文で使用できます。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># ジェネレータ関数
def generate_char():
    yield "A"
    yield "B"
    yield "C"


if __name__ == "__main__":
    # for文でのジェネレータ関数の使用
    for s in generate_char():
        print(s)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
A
B
C</pre>



<p>上記結果のようにジェネレータが生成する値（<code>"A"</code>、<code>"B"</code>、<code>"C"</code>）を順に取り出せていることが分かります。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="range関数と同じ処理を自分で書いてみる"><code>range</code> をジェネレータ関数で実現する</h3>



<p>Pythonの<code>for</code>文では、数値シーケンスの生成に<code>range</code>をよく使用します。<code>range</code>はジェネレータ関数として実現されています。</p>



<p>ジェネレータ関数の使用方法を覚えるために<code>range</code>と同じ動作をするジェネレータ関数を作ってみましょう。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group=""># ジェネレータ関数でrangeと同じ動作を実現
def sample_range(start=0, stop=10, step=1):
    num = start
    while num &lt; stop:
        yield num
        num += step


if __name__ == "__main__":
    iter_range = sample_range(0, 5)
    print(type(iter_range))
    print(next(iter_range))
    print(next(iter_range))
    print(next(iter_range))

    print("======================")
    for i in iter_range:
        print(i)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
&lt;class 'generator'>
0
1
2
======================
3
4</pre>



<p>上記の<code>sample_range</code>関数は、<code>range</code>と同じ動作をするジェネレータ関数です。<code>iter_range</code>は、ジェネレータのオブジェクト（<code>&lt;class 'generator'&gt;</code>）となっています。</p>



<p>そのため、<code>next</code>で値を順に取り出せていることが分かります。また、途中で「<code>"==="</code>」のような<code>print</code>の中断を入れていますが、次の呼び出しでは後続の値から取り出しができていることが分かります。</p>



<p>ジェネレータ関数を使用することで、一度にメモリ上に展開できないような無限に続くような数列なども実現可能になります。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="ジェネレータ内包表記">ジェネレータ内包表記</h2>



<p>ジェネレータは、ジェネレータ内包表記というシンプルな記載方法でも生成することができます。ジェネレータ内包表記については「<a href="https://tech.nkhn37.net/python-generator-comprehension/" target="_blank" rel="noreferrer noopener">ジェネレータ内包表記の使い方</a>」を参考にしてください。</p>



<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p>Pythonの<span class="marker"><strong>ジェネレータ関数とジェネレータの基本</strong></span>について解説しました。</p>



<p>ジェネレータ関数は、<code>yield</code> 文を使って値を返却する関数であり、ジェネレータオブジェクトを作成します。ジェネレータはイテレータの一種なので逐次的に値を生成することができます。この記事では、ジェネレータ関数の定義方法の例とジェネレータの使用例を紹介しました。</p>



<p>ジェネレータは一度にメモリ上に展開できないような無限に続くシーケンスを扱うこともできて便利です。ぜひ、使い方を覚えてもらいたいと思います。</p>



<section class="wp-block-jinr-blocks-simplebox b--jinr-block-container"><div class="b--jinr-block b--jinr-box d--heading-box8  "><div class="a--simple-box-title d--bold">ソースコード</div><div class="c--simple-box-inner">
<p>上記で紹介しているソースコードは <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-basic/function/generator" target="_blank" rel="noreferrer noopener">GitHub</a> にて公開しています。参考にしていただければと思います。</p>
</div></div></section>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/python-generator-yield/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>【Python】内包表記（まとめ）</title>
		<link>https://tech.nkhn37.net/python-comprehension/</link>
					<comments>https://tech.nkhn37.net/python-comprehension/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Tue, 26 Jan 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[Python入門]]></category>
		<category><![CDATA[Pythonic]]></category>
		<category><![CDATA[ジェネレータ内包表記]]></category>
		<category><![CDATA[パイソニック]]></category>
		<category><![CDATA[リスト内包表記]]></category>
		<category><![CDATA[内包表記]]></category>
		<category><![CDATA[辞書内包表記]]></category>
		<category><![CDATA[集合内包表記]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=525</guid>

					<description><![CDATA[Pythonにおける内包表記に関してまとめます。 Pythonにおける内包表記 Pythonにおいて、内包表記とは既存のリスト等のイテラブルなオブジェクトから新しいオブジェクトを生成する際にシンプルに記載するための定義す [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Pythonにおける<span class="marker"><strong>内包表記</strong></span>に関してまとめます。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="pythonにおける内包表記">Pythonにおける内包表記</h2>



<p>Pythonにおいて、<span class="marker"><strong>内包表記</strong></span>とは既存のリスト等のイテラブルなオブジェクトから新しいオブジェクトを生成する際にシンプルに記載するための定義する方法のことを言います。</p>



<p>内包表記の種類としては「<span class="marker"><strong>リスト内包表記</strong></span>」「<span class="marker"><strong>辞書内包表記</strong></span>」「<span class="marker"><strong>集合内包表記</strong></span>」「<span class="marker"><strong>ジェネレータ内包表記</strong></span>」といったものがあります。</p>



<p>この記事では、内包表記の特徴や使用上の注意点を紹介するとともに、各内包表記について紹介をします。各内包表記については、個別ページを作成しているため、この記事ではリンク先をご紹介します。</p>



<h3 class="wp-block-heading jinr-heading d--bold">内包表記の特徴</h3>



<p>Pythonの<span class="marker"><strong>内包表記</strong></span>については、以下のような特徴があります。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>特徴</th><th>概要</th></tr></thead><tbody><tr><td>コードを簡潔に記載ができる</td><td>ループや条件分岐を1行で記載することができるため、コードの可読性が向上します。</td></tr><tr><td>パフォーマンスの向上</td><td>内包表記は、Python内部で効率的な最適化が行われるため、通常は同等の<code>for</code>ループよりも高速です。</td></tr><tr><td>構文の一貫性</td><td>リスト、辞書、集合、ジェネレータといった様々なデータ型で一貫した構文が提供されています。</td></tr></tbody></table></figure>



<p>なお、ジェネレータ内包表記に関しては、一度にすべての要素をメモリに保持せず必要な時に要素を生成することができるため、メモリを有効に活用できる特徴もあります。</p>



<h3 class="wp-block-heading jinr-heading d--bold">内包表記の使用上の注意点</h3>



<p>内包表記は、処理をシンプルに記述できる点が特徴的です。また、Python内部で最適化されるため、一般的に実行速度も速くなります。処理速度は状況によって異なり、必ずしも常に早いとは限らないため、その点には注意が必要です。</p>



<p>また、内包表記がコードを複雑にする場合には、使用を慎重に検討してください。特にチーム開発では、ソースコードの可読性が重要ですので、シンプルさと可読性のバランスを保つことが重要です。</p>



<h3 class="wp-block-heading jinr-heading d--bold">様々な内包表記</h3>



<p>Pythonの内包表記には、リスト、辞書、集合、ジェネレータを対象としたものがあります。以下に各内包表記に関する使い方をまとめていますので参考にしてください。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>内包表記の種類</th><th>説明ページ</th></tr></thead><tbody><tr><td>リスト内包表記</td><td><a href="https://tech.nkhn37.net/python-list-comprehension/" target="_blank" rel="noreferrer noopener">リスト内包表記の使い方</a></td></tr><tr><td>辞書内包表記</td><td><a href="https://tech.nkhn37.net/python-dict-comprehension/" target="_blank" rel="noreferrer noopener">辞書内包表記の使い方</a></td></tr><tr><td>集合内包表記</td><td><a href="https://tech.nkhn37.net/python-set-comprehension/" target="_blank" rel="noreferrer noopener">集合内包表記の使い方</a></td></tr><tr><td>ジェネレータ内包表記</td><td><a href="https://tech.nkhn37.net/python-generator-comprehension/" target="_blank" rel="noreferrer noopener">ジェネレータ内包表記の使い方</a></td></tr></tbody></table></figure>



<h2 class="wp-block-heading jinr-heading d--bold" id="内包表記使用時の注意点">まとめ</h2>



<p>Pythonにおける<span class="marker"><strong>内包表記</strong></span>に関してまとめました。内包表記には、種類として「リスト内包表記」「辞書内包表記」「集合内包表記」「ジェネレータ内包表記」といったものがあります。</p>



<p>この記事では、内包表記の特徴や注意点を紹介しました。また、各内包表記への説明ページリンクも紹介しています。</p>



<p>Pythonらしいシンプルで読みやすいコードの書き方を「Pythonic（パイソニック）」なコードと言います。内包表記は、Pythonicなコーディングとして代表的なテクニックの一つです。「<a href="https://tech.nkhn37.net/pythonic-program-coding/" target="_blank" rel="noreferrer noopener">Pythonicなプログラムコーディング</a>」もぜひ参考にしてください。</p>



<p>ぜひ、各種内包表記の使い方を覚えて使いこなせるようになってもらいたいと思います。</p>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/python-comprehension/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-05-14 09:56:12 by W3 Total Cache
-->