Python 3000,zip,* args和迭代器 [英] Python 3000, zip, *args and iterators

查看:62
本文介绍了Python 3000,zip,* args和迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,据我所知,在Python 3000中,zip将基本上用izip替换为
,这意味着它不会返回一个列表,而是返回一个

迭代器。这适用于以下情况:


zip(* [iter1,iter2,iter3])


我希望收到的元组(来自

iterables的item1,item2,item3)。但它不适用于以下情况:


zip(* tuple_iter)


其中tuple_iter是元组的迭代器表单

(item1,item2,item3)我希望收到三个迭代器,一个到

item1s,一个到item2s,一个到item3s。我不认为这个

太不合理了当前拉链的欲望,在这样的情况下:


zip(* tuple_list)


其中tuple_list是表单元组的列表(item1,item2,item3),

返回三个元组的列表,其中一个是item1,其中一个是item2s和

其中一个item3s。


当然,这个目前不起作用的原因是fn(* itr)

表示法将''itr''转换为元组,耗尽迭代器:

So, as I understand it, in Python 3000, zip will basically be replaced
with izip, meaning that instead of returning a list, it will return an
iterator. This is great for situations like:

zip(*[iter1, iter2, iter3])

where I want to receive tuples of (item1, item2, item3) from the
iterables. But it doesn''t work well for a situation like:

zip(*tuple_iter)

where tuple_iter is an iterator to tuples of the form
(item1, item2, item3) and I want to receive three iterators, one to the
item1s, one to the item2s and one to the item3s. I don''t think this
is too unreasonable of a desire as the current zip, in a situation like:

zip(*tuple_list)

where tuple_list is a list of tuples of the form (item1, item2, item3),
returns a list of three tuples, one of the item1s, one of the item2s and
one of the item3s.

Of course, the reason this doesn''t work currently is that the fn(*itr)
notation converts ''itr'' into a tuple, exhausting the iterator:

def g(x):
.... for x in xrange(x):

.... yield(i,i + 1,i + 2)

....打印耗尽

.... zip(* g(4))
耗尽

[(0,1 ,2,3),(1,2,3,4),(2,3,4,5)] it.izip(* g(4))
耗尽

< ; itertools.izip对象位于0x01157710> x,y,z = it.izip(* g(4))
耗尽x,y,z
((0,1,2,3),(1,2,3,4) ,(2,3,4,5))


我更喜欢的是:

zip(* g(4))
< iterator object at ...> x,y,z = zip(* g(4))
x,y,z
(< iterator object at ...>,< iterator object at ...,<迭代器对象在......)


当然,我可以写一个单独的函数来做我想做的事情

这里[1] - 我的问题如果Python的内置zip将支持这个在
Python 3000中。它肯定不是一个微不足道的改变 - 它需要一些

相当大幅向后不兼容的变化在* args是如何为函数调用解析
- 即fn(* itr)只在必要时提取尽可能多的

项目,例如

def h(x,y,* args):
.... print x,y,args

.... print list(it.islice) (args,4))

.... h(* it.count())
def g(x): .... for i in xrange(x):
.... yield (i, i+1, i+2)
.... print "exhausted"
.... zip(*g(4)) exhausted
[(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)] it.izip(*g(4)) exhausted
<itertools.izip object at 0x01157710> x, y, z = it.izip(*g(4)) exhausted x, y, z ((0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5))

What I would prefer is something like:
zip(*g(4)) <iterator object at ...> x, y, z = zip(*g(4))
x, y, z (<iterator object at ...>, <iterator object at ..., <iterator object at ...)

Of course, I can write a separate function that will do what I want
here[1] -- my question is if Python''s builtin zip will support this in
Python 3000. It''s certainly not a trivial change -- it requires some
pretty substantially backwards incompatible changes in how *args is
parsed for a function call -- namely that fn(*itr) only extracts as many
of the items in the iterable as necessary, e.g.
def h(x, y, *args): .... print x, y, args
.... print list(it.islice(args, 4))
.... h(*it.count())



0 1 count (2)

[2,3,4,5]


所以我想我真正的问题是,我应该期待Python 3000能够播放吗? >
很好用* args和迭代器?是否有理由(除了向后的

不兼容)解析* args这种方式会不好?

Steve

[1]事实上,在这个列表的帮助下,我做了:
http://aspn.activestate.com/ASPN/Coo.../Recipe/302325

推荐答案


" Steven Bethard" < ST ************ @ gmail.com>在消息中写道

news:3yGzd.289258

"Steven Bethard" <st************@gmail.com> wrote in message
news:3yGzd.289258


HA.962@attbi_s01 ...
HA.962@attbi_s01...
所以,据我所知,在Python 3000中,zip将基本上用izip替换,这意味着它不会返回一个列表,而是返回一个
迭代器。
So, as I understand it, in Python 3000, zip will basically be replaced
with izip, meaning that instead of returning a list, it will return an
iterator.




我认为值得重复的是,Python 3还是一个什么样的
pipedream,正如笑话名称Python 3000所表示的那样(在Windows上也是如此
部分讽刺) 2000年之类的)。所以,虽然Guido已经说过他希望以某种方式使面向Python迭代器的方式成为面向列表的b
b,但肯定没有什么是一成不变的,当然不是细节。


Guido还表示,他希望有资金支付给他一年的开发费用。他想要花那么长时间,所以会有足够的讨论,思考和测试,所以他可以把它弄好,因为它具有最少的b / b
一切都很好。


Terry J. Reedy



I think it worth repeating that Python 3 is at yet something of a
pipedream, as indicated by the joke name Python 3000 (that also being in
part a satire on Windows 2000, and the like). So, while Guido has said he
would like to make Python iterator-oriented in the way that it used to be
list-oriented, nothing is set in stone, certainly not the details.

Guido has also said that he would like there to be funding to pay him to
spend a year on its development. He wants to take that long so there will
be adequate discussion, thought, and testing so he can ''get it right'' as
least in the sense of having everything work well together.

Terry J. Reedy


Terry Reedy写道:
Terry Reedy wrote:
Steven Bethard < ST ************ @ gmail.com>在消息中写道
新闻:3yGzd.289258
"Steven Bethard" <st************@gmail.com> wrote in message
news:3yGzd.289258


这篇关于Python 3000,zip,* args和迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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