나중에 쓰려고 쌓아두고 있던 간단한 예시인데, 더 이상 쌓이지가 않아서 (–;)
방출해 봅니다;
파이썬 이터레이터/제너레이터는 함축적으로 프로그래밍하는 걸 즐기는 사람들에게는
정말 재미있지만, 절차적 프로그래밍을 하다가 갑자기 이터레이터를 쓰는 게 적응이
잘 안 되는 경우도 많이 볼 수 있습니다. 최고의(!) 파이썬 책으로
유명한 David Beazley가 지난 3월에 PyCon 2008에서 튜토리얼로
시스템 프로그래머를 위한 제너레이터 트릭을 발표했습니다. 여러모로 재미있는 내용인데요~ 저기서는 제너레이터를
주로 다뤘는데, 몇 가지 이터레이터와 관련된 사용 예를 소개해 드리려고요~
(별로 관련 없는 것이 뒤죽박죽;;)
zip을 쓰면 이터레이터를 일정 개수씩 빼오는 걸 간단하게 할 수 있습니다.
1 2 3 4 5 |
<span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="s">'abcdefghijklmnopqrstvwxyz'</span><span class="p">)</span> <span class="o">>>></span> <span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mf">5</span><span class="p">),</span> <span class="n">s</span><span class="p">)</span> <span class="p">[(</span><span class="mf">0</span><span class="p">,</span> <span class="s">'a'</span><span class="p">),</span> <span class="p">(</span><span class="mf">1</span><span class="p">,</span> <span class="s">'b'</span><span class="p">),</span> <span class="p">(</span><span class="mf">2</span><span class="p">,</span> <span class="s">'c'</span><span class="p">),</span> <span class="p">(</span><span class="mf">3</span><span class="p">,</span> <span class="s">'d'</span><span class="p">),</span> <span class="p">(</span><span class="mf">4</span><span class="p">,</span> <span class="s">'e'</span><span class="p">)]</span> <span class="o">>>></span> <span class="nb">zip</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mf">5</span><span class="p">),</span> <span class="n">s</span><span class="p">)</span> <span class="p">[(</span><span class="mf">0</span><span class="p">,</span> <span class="s">'f'</span><span class="p">),</span> <span class="p">(</span><span class="mf">1</span><span class="p">,</span> <span class="s">'g'</span><span class="p">),</span> <span class="p">(</span><span class="mf">2</span><span class="p">,</span> <span class="s">'h'</span><span class="p">),</span> <span class="p">(</span><span class="mf">3</span><span class="p">,</span> <span class="s">'i'</span><span class="p">),</span> <span class="p">(</span><span class="mf">4</span><span class="p">,</span> <span class="s">'j'</span><span class="p">)]</span> |
빈칸이 올 때까지 입력받은 걸 리스트로 받으려면?
1 2 3 4 5 |
<span class="o">>>></span> <span class="nb">list</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="nb">raw_input</span><span class="p">,</span> <span class="s">''</span><span class="p">))</span> <span class="n">Python</span> <span class="o">-</span> <span class="n">batteries</span> <span class="n">included</span> <span class="p">[</span><span class="s">'Python - '</span><span class="p">,</span> <span class="s">'batteries included'</span><span class="p">]</span> |
딕셔너리 2개에서 양쪽 중 하나라도 키가 있는 것들을 뽑아서 돌려서 합치려는 경우..
1 2 3 4 5 6 |
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="p">{</span><span class="s">'a'</span><span class="p">:</span> <span class="mf">3</span><span class="p">,</span> <span class="s">'b'</span><span class="p">:</span> <span class="mf">4</span><span class="p">}</span> <span class="o">>>></span> <span class="n">b</span> <span class="o">=</span> <span class="p">{</span><span class="s">'b'</span><span class="p">:</span> <span class="mf">5</span><span class="p">,</span> <span class="s">'c'</span><span class="p">:</span> <span class="mf">6</span><span class="p">}</span> <span class="o">>>></span> <span class="n">set</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">|</span> <span class="n">set</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="n">set</span><span class="p">([</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">])</span> <span class="o">>>></span> <span class="nb">dict</span><span class="p">((</span><span class="n">key</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="mf">0</span><span class="p">)</span> <span class="o">+</span> <span class="n">b</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="mf">0</span><span class="p">))</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">set</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">|</span> <span class="n">set</span><span class="p">(</span><span class="n">b</span><span class="p">))</span> <span class="p">{</span><span class="s">'a'</span><span class="p">:</span> <span class="mf">3</span><span class="p">,</span> <span class="s">'c'</span><span class="p">:</span> <span class="mf">6</span><span class="p">,</span> <span class="s">'b'</span><span class="p">:</span> <span class="mf">9</span><span class="p">}</span> |
(이 경우는 collections.defaultdict를 써도 간단합니다.)
리스트를 받아서 순위를 계산
1 2 3 4 |
<span class="o">>>></span> <span class="k">from</span> <span class="nn">operator</span> <span class="k">import</span> <span class="o">*</span> <span class="o">>>></span> <span class="n">s</span> <span class="o">=</span> <span class="p">[</span><span class="mf">9</span><span class="p">,</span> <span class="mf">8</span><span class="p">,</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">3</span><span class="p">,</span> <span class="mf">7</span><span class="p">,</span> <span class="mf">4</span><span class="p">,</span> <span class="mf">0</span><span class="p">]</span> <span class="o">>>></span> <span class="nb">map</span><span class="p">(</span><span class="n">itemgetter</span><span class="p">(</span><span class="mf">0</span><span class="p">),</span> <span class="n">sorted</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">s</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="n">itemgetter</span><span class="p">(</span><span class="mf">1</span><span class="p">),</span> <span class="n">reverse</span><span class="o">=</span><span class="bp">True</span><span class="p">))</span> <span class="p">[</span><span class="mf">0</span><span class="p">,</span> <span class="mf">1</span><span class="p">,</span> <span class="mf">5</span><span class="p">,</span> <span class="mf">3</span><span class="p">,</span> <span class="mf">6</span><span class="p">,</span> <span class="mf">4</span><span class="p">,</span> <span class="mf">2</span><span class="p">,</span> <span class="mf">7</span><span class="p">]</span> |
(이건 뭐 주제하고 상관도 없고 제멋대로;;;)
코드에 줄바꿈이 없어서 처음에 이해하기가 힘들었네요 ^^;
그래도 유용한 정보 감사합니다.
코드블럭을 잘못 잡아서 RSS에서 줄 바꿈이 안 돼 있었네요 ^.^; 수정했습니다~