Converting a flat list to a list of tuples
Bengt Richter
bokr at oz.net
Tue Nov 22 15:54:56 EST 2005
On Tue, 22 Nov 2005 14:28:56 GMT, "David Isaac" <aisaac0 at verizon.net> wrote:
>
>"Duncan Booth" <duncan.booth at invalid.invalid> wrote in message
>news:Xns971671CB0DA43duncanbooth at 127.0.0.1...
>> >>> aList = ['a', 1, 'b', 2, 'c', 3]
>> >>> it = iter(aList)
>> >>> zip(it, it)
>> [('a', 1), ('b', 2), ('c', 3)]
>
>That behavior is currently an accident.
>http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
>Alan Isaac
>
That says
"""
ii. The other problem is easier to explain by example.
Let it=iter([1,2,3,4]).
What is the result of zip(*[it]*2)?
The current answer is: [(1,2),(3,4)],
but it is impossible to determine this from the docs,
which would allow [(1,3),(2,4)] instead (or indeed
other possibilities).
"""
IMO left->right is useful enough to warrant making it defined behaviour,
not an accident. Isn't it(),it() well defined for a given iterator?
So is the question whether zip will access its referenced input iterators
in some peculiar order? Is the order of zip(a,b) accesses to a and b
undefined? If, so, IMO it's reasonable to make it defined as if
>>> def zip(*args):
... return list(tuple([it.next() for it in its])
... for its in [[iter(a) for a in args]]
... for _ in iter(lambda:0,1))
...
>>> aList = ['a', 1, 'b', 2, 'c', 3]
>>> it = iter(aList)
>>> zip(it, it)
[('a', 1), ('b', 2), ('c', 3)]
>>> it = iter(aList)
>>> zip(it, it, it)
[('a', 1, 'b'), (2, 'c', 3)]
>>> it = iter(aList)
>>> zip(it)
[('a',), (1,), ('b',), (2,), ('c',), (3,)]
>>> zip(range(3), range(4))
[(0, 0), (1, 1), (2, 2)]
>>> zip(range(4), range(3))
[ (0, 0), (1, 1), (2, 2)]
(I just hacked this out, so maybe it's not bullet-proof, but the point is,
I think there's no reason not to define the behaviour of zip to cycle
through its arguments in the intuitive way).
Regards,
Bengt Richter
More information about the Python-list
mailing list