I am trying to import the izip module like so:
我正在尝试导入izip模块:
from itertools import izip
However after recently changing over from Python 2.7 to 3 - it doesn't seem to work.
然而,在最近从Python 2.7转换到3之后,它似乎并不起作用。
I am trying to write to a csv file:
我试着写一个csv文件:
writer.writerows(izip(variable1,2))
But I have no luck. Still encounter an error.
但是我没有运气。还遇到一个错误。
2 个解决方案
#1
30
In Python 3 the built-in zip
does the same job as itertools.izip
in 2.x (returns an iterator instead of a list). The zip
implementation is almost completely copy-pasted from the old izip
, just with a few names changed and pickle support added.
在Python 3中,内置的zip与itertools做同样的工作。izip在2。返回一个迭代器而不是列表。zip实现几乎完全复制了旧的izip,只是添加了一些名称和pickle支持。
Here is a benchmark between zip
in Python 2 and 3 and izip
in Python 2:
这里是Python 2和3之间的zip和Python 2中的izip之间的基准:
Python 2.7:
from timeit import timeit
print(timeit('list(izip(xrange(100), xrange(100)))',
'from itertools import izip',
number=500000))
print(timeit('zip(xrange(100), xrange(100))', number=500000))
Output:
输出:
1.9288790226
1.2828938961
Python 3:
from timeit import timeit
print(timeit('list(zip(range(100), range(100)))', number=500000))
Output:
输出:
1.7653984297066927
In this case since zip
's arguments must support iteration you can not use 2 as its argument. So if you want to write 2 variable as a CSV row you can put them in a tuple or list:
在本例中,由于zip的参数必须支持迭代,所以不能使用2作为其参数。如果你想把2个变量写成CSV行你可以把它们放到一个元组或列表中:
writer.writerows((variable1,2))
Also from itertools
you can import zip_longest
as a more flexible function which you can use it on iterators with different size.
同样,您可以从itertools导入zip_最长作为一个更灵活的函数,您可以在不同大小的迭代器上使用它。
#2
0
One of the ways which helped me is:
其中一个帮助我的方法是:
try:
from itertools import izip as zip
except ImportError: # will be 3.x series
pass
#1
30
In Python 3 the built-in zip
does the same job as itertools.izip
in 2.x (returns an iterator instead of a list). The zip
implementation is almost completely copy-pasted from the old izip
, just with a few names changed and pickle support added.
在Python 3中,内置的zip与itertools做同样的工作。izip在2。返回一个迭代器而不是列表。zip实现几乎完全复制了旧的izip,只是添加了一些名称和pickle支持。
Here is a benchmark between zip
in Python 2 and 3 and izip
in Python 2:
这里是Python 2和3之间的zip和Python 2中的izip之间的基准:
Python 2.7:
from timeit import timeit
print(timeit('list(izip(xrange(100), xrange(100)))',
'from itertools import izip',
number=500000))
print(timeit('zip(xrange(100), xrange(100))', number=500000))
Output:
输出:
1.9288790226
1.2828938961
Python 3:
from timeit import timeit
print(timeit('list(zip(range(100), range(100)))', number=500000))
Output:
输出:
1.7653984297066927
In this case since zip
's arguments must support iteration you can not use 2 as its argument. So if you want to write 2 variable as a CSV row you can put them in a tuple or list:
在本例中,由于zip的参数必须支持迭代,所以不能使用2作为其参数。如果你想把2个变量写成CSV行你可以把它们放到一个元组或列表中:
writer.writerows((variable1,2))
Also from itertools
you can import zip_longest
as a more flexible function which you can use it on iterators with different size.
同样,您可以从itertools导入zip_最长作为一个更灵活的函数,您可以在不同大小的迭代器上使用它。
#2
0
One of the ways which helped me is:
其中一个帮助我的方法是:
try:
from itertools import izip as zip
except ImportError: # will be 3.x series
pass