LoginSignup
2
3

More than 3 years have passed since last update.

備忘録: Python 関数 zip() の引数にイテレータを使うと位置によって挙動が変わる (あたりまえの) 話

Last updated at Posted at 2018-08-19

ひとつのイテレータを複数の箇所で使いまわそうとしてまして。

>>> from itertools import count
>>> fooiter = count()
>>> tuple(zip(fooiter, ('a', 'b', 'c')))
((0, 'a'), (1, 'b'), (2, 'c'))
>>> tuple(zip(fooiter, ('a', 'b', 'c')))
((4, 'a'), (5, 'b'), (6, 'c'))

イテレータ (count()) が 3 を飛ばす。
なぜなら第2引数のタプルの終端を検出する前に第1引数のイテレータを評価 (next())するから第1引数を最後に評価した値 3 (そして7も) は破棄されてしまう。

>>> tuple(zip(('a', 'b', 'c'), fooiter))
(('a', 0), ('b', 1), ('c', 2))
>>> tuple(zip(('a', 'b', 'c'), fooiter))
(('a', 3), ('b', 4), ('c', 5))
>>> 

インデックスが後ろについて気持ち悪いがそれは気持ちの問題。

「イテラブルの左から右への評価順序は保証されています。」
ちょっと考えればすぐわかる話(←強調)。

2
3
0

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
2
3