izip的逆 [英] inverse of izip

查看:89
本文介绍了izip的逆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道zip(*)是zip()的反转,例如:

zip (* zip(范围(10),范围(10)))
[(0,1,2,3,4,5,6,7,8,9),(0,1,2,3) ,4,5,6,7,8,9)]


izip的反转是什么?当然,我可以使用zip(*)或izip(*),

例如:

zip(* itertools.izip(范围(10),范围(10) ))
[(0,1,2,3,4,5,6,7,8,9),(0,1,2,3,4,5,6,7,8,9) ] x,y = itertools.izip(* itertools.izip(范围(10),范围(10)))
x,y



((0,1,2,3,4,5,6,7,8,9),(0,1,2,3,4,5,6,7,8,9)) >

然后我得到一对元组,而不是一对迭代器。基本上,

我想将元组的迭代器转换为迭代器的元组。


Steve

解决方案



Steven Bethard写道:

所以我知道zip(*)是zip()的反转,例如:

zip(* zip(范围(10),范围(10)))

[(0,1, 2,3,4,5,6,7,8,9),(0,1,2,3,4,5,6,7,8,9)]

什么''是izip的反转?当然,我可以使用zip(*)或izip(*),
例如:

zip(* itertools.izip(范围(10),范围(10))) /(
[(0,1,2,3,4,5,6,7,8,9),(0,1,2,3,4,5,6,7,8,9) ]

x,y = itertools.izip(* itertools.izip(范围(10),范围(10)))
x,y

(( 0,1,2,3,4,5,6,7,8,9),(0,1,2,3,4,5,6,7,8,9))
但后来我得到了一对元组,而不是一对迭代器。基本上,
我想将元组的迭代器转换为迭代器的元组。

史蒂夫



----- ----------------------------

a = itertools.izip(* itertools.izip (范围(10),范围(10)))
$
< itertools.izip对象位于0x40164f2c>

a.next()
(0, 1,2,3,4,5,6,7,8,9)a.next()
(0,1,2,3,4,5,6,7,8,9)a。 next()



Traceback(最近一次调用最后一次):

文件"< stdin>",line 1,在?

StopIteration


------------------------- ----

问候,

Satchit

----

Satchidanand Haridas(zeomega dot的sharidas) com)


ZeOmega( www.zeomega.com

开放思想'开放式解决方案

#20,Rajalakshmi Plaza,

South End Road,

Basav anagudi,

Bangalore-560 004,印度


2004年8月19日星期四12:07:48 + 0530,Satchidanand Haridas

< sh ****** @ zeomega.com>写道:

a = itertools.izip(* itertools.izip(range(10),range(10))) /> a< itertools.izip对象位于0x40164f2c>
a.next()(0,1,2,3,4,5,6,7,8,9)a.next()(0, 1,2,3,4,5,6,7,8,9)a.next()


我假设你没有真正阅读我的

电子邮件。不用担心 - 它有时会发生。但是你会注意到,

这正是我所说的无效:


Steven Bethard写道:x,y = itertools。 izip(* itertools.izip(范围(10),范围(10)))
x,y


((0,1,2,3,4,5) ,6,7,8,9),(0,1,2,3,4,5,6,7,8,9))

然后我得到一对元组,而不是一对迭代器。基本上,
我想将元组的迭代器转换为迭代器的元组。




我希望itertools.izip对象返回的元素为

迭代器,而不是元组或列表。


Steve


-

你如果你只是动词就可以说明任何事情。

- Bucky Katt,Get Fuzzy


Steven Bethard< steven.bethard< at> gmail.com>写道:

izip的反转是什么?当然,我可以使用zip(*)或izip(*),
例如:

zip(* itertools.izip(range) (10),范围(10)))[(0,1,2,3,4,5,6,7,8,9),(0,1,2,3,4,5,6,7, 8,9)] x,y = itertools.izip(* itertools.izip(range(10),range(10)))
x,y((0,1,2,3,4,5, 6,7,8,9),(0,1,2,3,4,5,6,7,8,9))

然后我得到一对元组,而不是对迭代器。基本上,
我想将元组的迭代器转换为迭代器的元组。




很抱歉回复自己,但在玩了一下itertools之后

while,这似乎有效:

import itertools
starzip = lambda iterables :((tuple [i] for the tuple in itr)for i,itr in
enumerate(itertools.tee(iterables)))starzip(itertools.izip(range(10),range(10)))
< generator object at 0x008DED28> x,y = starzip(itertools.izip(range(10),range(10)))
x
< 0x008E1058>生成器对象> y
< 0x008E1080处的生成器对象> list(x)
[0,1,2,3,4,5,6,7,8,9] list(y)



[0,1,2,3,4,5,6,7,8,9]


看起来像izip的逆操作有点工作但是我'等等,看看是否有其他人有更好的解决方案。 (更不用说,如果我没有使用2.4,它就不会是一个单一的

线解决方案......)


Steve


So I know that zip(*) is the inverse of zip(), e.g.:

zip(*zip(range(10), range(10))) [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]

What''s the inverse of izip? Of course, I could use zip(*) or izip(*),
e.g.:
zip(*itertools.izip(range(10), range(10))) [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)] x, y = itertools.izip(*itertools.izip(range(10), range(10)))
x, y


((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

But then I get a pair of tuples, not a pair of iterators. Basically,
I want to convert an iterator of tuples into a tuple of iterators.

Steve

解决方案


Steven Bethard wrote:

So I know that zip(*) is the inverse of zip(), e.g.:

zip(*zip(range(10), range(10)))

[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]

What''s the inverse of izip? Of course, I could use zip(*) or izip(*),
e.g.:
zip(*itertools.izip(range(10), range(10)))

[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)]

x, y = itertools.izip(*itertools.izip(range(10), range(10)))
x, y

((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

But then I get a pair of tuples, not a pair of iterators. Basically,
I want to convert an iterator of tuples into a tuple of iterators.

Steve


---------------------------------

a = itertools.izip(*itertools.izip(range(10),range(10) ))
a <itertools.izip object at 0x40164f2c>
a.next() (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) a.next() (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) a.next()


Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration

-----------------------------
Regards,
Satchit
----
Satchidanand Haridas (sharidas at zeomega dot com)

ZeOmega (www.zeomega.com)
Open Minds'' Open Solutions

#20,Rajalakshmi Plaza,
South End Road,
Basavanagudi,
Bangalore-560 004, India


On Thu, 19 Aug 2004 12:07:48 +0530, Satchidanand Haridas
<sh******@zeomega.com> wrote:

a = itertools.izip(*itertools.izip(range(10),range(10) ))
a <itertools.izip object at 0x40164f2c>
a.next() (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) a.next() (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) a.next()
I''m assuming you popped this one off without actually reading my
email. No worries - it happens some times. You''ll note however, that
this is exactly what I said didn''t work:

Steven Bethard wrote:x, y = itertools.izip(*itertools.izip(range(10), range(10)))
x, y


((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

But then I get a pair of tuples, not a pair of iterators. Basically,
I want to convert an iterator of tuples into a tuple of iterators.



I want the elements returned by the itertools.izip object to be
iterators, not tuples or lists.

Steve

--
You can wordify anything if you just verb it.
- Bucky Katt, Get Fuzzy


Steven Bethard <steven.bethard <at> gmail.com> writes:

What''s the inverse of izip? Of course, I could use zip(*) or izip(*),
e.g.:

zip(*itertools.izip(range(10), range(10))) [(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)] x, y = itertools.izip(*itertools.izip(range(10), range(10)))
x, y ((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))

But then I get a pair of tuples, not a pair of iterators. Basically,
I want to convert an iterator of tuples into a tuple of iterators.



Sorry to respond to myself, but after playing around with itertools for a
while, this seems to work:

import itertools
starzip = lambda iterables: ((tuple[i] for tuple in itr) for i, itr in enumerate(itertools.tee(iterables))) starzip(itertools.izip(range(10), range(10))) <generator object at 0x008DED28> x, y = starzip(itertools.izip(range(10), range(10)))
x <generator object at 0x008E1058> y <generator object at 0x008E1080> list(x) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list(y)


[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Seems like a bit of work for the inverse of izip though so I''ll wait to see if
anyone else has a better solution. (Not to mention, it wouldn''t be a single
line solution if I wasn''t using 2.4...)

Steve


这篇关于izip的逆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆