zip in IronPython 2.7 and Python3.5
Asked Answered
O

3

6

I have a script that I want to execute both in Python3.5 and IronPython2.7.

The script is originally written with Python3 in mind, so I have some nested loops similar to the code below :

myIter0 = iter(['foo','foo','bar','foo','spam','spam'])
myIter1 = iter(['foo','bar','spam','foo','spam','bar'])
myIter2 = iter([1,2,3,4,5,6])

for a in myIter0:
    for b, c in zip(myIter1, myIter2):
        if a + b == 'foobar':
            print(c)
            break 

Now if I run this in IronPython2.7 I don't get the same result because zip returns a list and not an iterator.

To circumvent this problem, I thought I would do :

import sys
if sys.version_info.major == 2:
    from itertools import izip as _zip
else:
    _zip = zip

myIter0 = iter(['foo','foo','bar','foo','spam','spam'])
myIter1 = iter(['foo','bar','spam','foo','spam','bar'])
myIter2 = iter([1,2,3,4,5,6])

for a in myIter0:
    for b, c in _zip(myIter1, myIter2):
        if a + b == 'foobar':
            print(c)
            break 

Is there any better way to do this ?

Overlay answered 26/4, 2016 at 14:49 Comment(4)
Maybe I'm still too sleep, but what's the problem with zip returning a list in python2? Or is it specific to IronPython?Lolita
The problem is that zip(myIter1, myIter2) uses all the items in myIter1 and myIter2 in Python2. The program doesn't reenter the nested for loop since zip(myIter1, myIter2) is an empty iterator after the first iteration of the main loop.Overlay
How about six.zip()?Lunate
@pzp, that's a good suggestion but I am working from IronPython in Grasshopper, so I am looking for a single file solutions. Thanks for introducing me to six though.Overlay
H
3

You can use builtins from the future lib.

from builtins import zip

In python2 you get an itertools.izip and in python3 you just get zip.

In [1]: from builtins import zip

In [2]: zip([1,2,3,4])
Out[2]: <itertools.izip at 0x7fa16c8496c8>

In [1]: from builtins import zip

In [2]: zip([1,2,3,4])
Out[2]: <zip at 0x7f13dfb9c188>
Hyades answered 26/4, 2016 at 15:11 Comment(6)
@JacquesGaudin, you have to install it pip install future, if you want to write code for python2 and 3 then you should install itHyades
Actually from future_builtins import zip works without installing future !Overlay
@JacquesGaudin, it will work in python2 but not in python3, there is no future_builtins.Hyades
yes but I have a try statment to import other functions anyway. So if I put it in there, I can catch the ImportError when running in Python3Overlay
@JacquesGaudin, yes but the point is python-future is batteries included it does the work for you. python-future.org/overview.html#code-examplesHyades
I use IronPython from Grasshopper to provide some geometry methods that I don't use otherwise, hence the try statement.Overlay
I
2

That looks perfectly reasonable to me. A small modification will allow you to avoid the explicit version number check by doing

try:
    from itertools import izip as zip_
except ImportError:
    # Python 3
    zip_ = zip
Ignatzia answered 26/4, 2016 at 15:3 Comment(1)
Thanks. I actually have such a try statment at the beginning of my script to detect the availability of other functions so it would be easy to incorporate.Overlay
O
-1

For Python3.5 users,

A=[1,2,3,4,5,6,7]
B=[7,6,5,4,32,1]

In: c=list(zip(A,B))

In: print(c)

out: [(1,7),(2,6),(3,5),(4,4),(5,3),(6,2),(7,1)]
Omeromero answered 29/8, 2016 at 9:19 Comment(1)
This late answer doesn't relate to the question.Overlay

© 2022 - 2024 — McMap. All rights reserved.