J
Joost Cassee
Hello all,
I bumped into an unexpected problem using generators. Consider this code:
def test(s):
return _test([], [], s)
def _test(s1, s2, s):
if s:
s1.append(s.pop())
for x in _test(s1, s2, s):
yield x
s2.append(s1.pop())
for x in _test(s1, s2, s):
yield x
s.append(s2.pop())
else:
yield s1, s2
The test function is supposed be a generator returning all combinations
of dividing the elements of the input list into two distinct lists
(order of elements unimportant). (This post is not about the quality of
the solution. )
Consider now the result of using the generator in a for-loop and in the
list function (same result for list comprehension):
.... print x
....
([2, 1, 0], [])
([2, 1], [0])
([2, 0], [1])
([2], [1, 0])
([1, 0], [2])
([1], [2, 0])
([0], [2, 1])
([], [2, 1, 0])[([], []), ([], []), ([], []), ([], []), ([], []), ([], []), ([], []),
([], [])]
The following (simpler) generator works as expected:
.... for x in s:
.... yield x
....[0, 1, 2]
Can anyone explain the difference? The python version is 2.5.1.
Regards,
Joost
I bumped into an unexpected problem using generators. Consider this code:
def test(s):
return _test([], [], s)
def _test(s1, s2, s):
if s:
s1.append(s.pop())
for x in _test(s1, s2, s):
yield x
s2.append(s1.pop())
for x in _test(s1, s2, s):
yield x
s.append(s2.pop())
else:
yield s1, s2
The test function is supposed be a generator returning all combinations
of dividing the elements of the input list into two distinct lists
(order of elements unimportant). (This post is not about the quality of
the solution. )
Consider now the result of using the generator in a for-loop and in the
list function (same result for list comprehension):
.... print x
....
([2, 1, 0], [])
([2, 1], [0])
([2, 0], [1])
([2], [1, 0])
([1, 0], [2])
([1], [2, 0])
([0], [2, 1])
([], [2, 1, 0])[([], []), ([], []), ([], []), ([], []), ([], []), ([], []), ([], []),
([], [])]
The following (simpler) generator works as expected:
.... for x in s:
.... yield x
....[0, 1, 2]
Can anyone explain the difference? The python version is 2.5.1.
Regards,
Joost