multirember&安培;合 [英] multirember&co

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

问题描述

一旦进入,我也有问题。这是一个小问题

来自一个Scheme Book(我已经离开了这个帖子,因为这个

帖子包含太多用于Scheme新闻组的Python代码):
http://groups.google。 com / group / comp .... 9f78eb4457d08 /

函数multiremberandco很难(对我而言)如果完成那个

little Scheme子集,但使用Python非常容易。它从给定的列表中收集两个

列表,最后它将通用的给定乐趣

函数应用于两个收集的列表并返回其结果:


def multiremberandco1(el,seq,fun):

l1,l2 = [],[]

for x in seq:

如果x == el:

l2.append(el)

else:

l1.append(el)

返回乐趣(l1,l2)


data = [1,''a'',3,'''',4,5,6 ,'''']

打印multiremberandco1('''',数据,lambda l1,l2:(len(l1),len(l2)))

更紧凑:


def multiremberandco2(el,seq,fun):

l1,l2 = [],[]

for x in seq:

[l1,l2] [x == el] .append(el)

return fun(l1,l2)

有点干净(但我不太喜欢):


def multiremberandco3(el,seq,fun):

l1,l2 = [],[]

for x in seq:

(l2如果x == el else l1).append(el)

返回乐趣(l1,l2)

为了好玩,我试图让它变得懒惰,如果seq是一个很长的可迭代的b / b
可能有用。所以我用过tee:


来自itertools import ifilter,tee


def multiremberandco4(el,iseq,fun):

iseq1,iseq2 = tee(iseq)

iterable1 = ifilter(lambda x:x == el,iseq1)

iterable2 = ifilter(lambda x :x!= el,iseq2)

返回乐趣(iterable1,iterable2)


def leniter(seq):

count = 0

for el in seq:

count + = 1

返回计数


idata = iter(数据)

打印multiremberandco4('''',idata,lambda l1,l2 :( leniter(l1),

leniter(l2)))

但是从文档:>一般来说,如果一个迭代器将使用大多数

或其他迭代器之前的所有数据,那么使用它会更快/>
list()而不是tee()。​​<


所以我试图为有趣的函数扫描创建两个iterables

seq一次,但我到目前为止还没有成功(要做到这一点,我已经尝试过

使用两个corout使用增强型发电机,根据x == el的值将el发送到另一个或

到另一个,这次我没有显示

我失败了你有什么建议吗?


(注意:在某些情况下,创建一个给定可迭代的拆分

函数可能会有用和一个适合的谓词,返回两个

懒惰生成器,满足谓词的项目和不满足它的

项目,所以这个练习不是'完全没用了。


再见,

熊宝宝

解决方案

成为************ @ lycos.com 写道:


一旦进入,我也有事要问。这是一个小问题

来自一个Scheme Book(我已经离开了这个帖子,因为这个

帖子包含太多用于Scheme新闻组的Python代码):
http://groups.google。 com / group / comp .... 9f78eb4457d08 /

函数multiremberandco很难(对我而言)如果完成那个

little Scheme子集,但使用Python非常容易。它从给定的列表中收集两个

列表,最后它将通用的给定乐趣

函数应用于两个收集的列表并返回其结果:


def multiremberandco1(el,seq,fun):

l1,l2 = [],[]

for x in seq:

如果x == el:

l2.append(el)

else:

l1.append(el)

返回乐趣(l1,l2)


data = [1,''a'',3,'''',4,5,6 ,'''']

打印multiremberandco1('''',数据,lambda l1,l2:(len(l1),len(l2)))

更紧凑:


def multiremberandco2(el,seq,fun):

l1,l2 = [],[]

for x in seq:

[l1,l2] [x == el] .append(el)

return fun(l1,l2)< br $> b
$ b有点清洁(但我不喜欢):


def multiremberandco3(el,seq,fun):

l1,l2 = [],[]

for x in seq:

(l2如果x == el else l1).append(el)

返回乐趣(l1,l2)


为了好玩,我试图让它变得懒惰,如果seq是非常长的可迭代的话,可能会很有用。所以我用过tee:


来自itertools import ifilter,tee


def multiremberandco4(el,iseq,fun):

iseq1,iseq2 = tee(iseq)

iterable1 = ifilter(lambda x:x == el,iseq1)

iterable2 = ifilter(lambda x :x!= el,iseq2)

返回乐趣(iterable1,iterable2)


def leniter(seq):

count = 0

for el in seq:

count + = 1

返回计数


idata = iter(数据)

打印multiremberandco4('''',idata,lambda l1,l2 :( leniter(l1),

leniter(l2)))


但是从文档:>一般情况下,如果一个迭代器将使用大多数

或其他迭代器之前的所有数据,它会更快使用

list()而不是tee()。​​<


所以我试图为有趣的函数扫描创建两个iterables

seq一次,但我到目前为止还没有成功(做到这一点,哈哈我试图使用增强型发电机使用两个协程,根据x == el的值将el发送到另一个或

到另一个,这次我不知道我的失败版本显示

,你有建议吗?


(注意:在某些情况下,创建一个拆分可能是有用的。

函数给出一个可迭代的谓词,返回两个

懒惰生成器,满足谓词和

项的项目不满足它,所以这个练习并不是完全无用的。


再见,

熊宝宝



我认为可能证明这个

练习需要两个迭代器。在这个例子中,例如,leniter()将在一个

列表上调用,然后在另一个列表上调用。如何获得一个列表的准确计数

而不迭代另一个?


即:


data = [1,'''',3,''a'',4,5,6,'''''

^

来算一算这个'''',它必须经过

数字,因此leniter()不能使用单个迭代器在列表上独立行动

。 />

James


4月17日凌晨1点14分,bearophileH ... @ lycos.com写道:
< blockquote class =post_quotes>
一旦进入,我也有问题要问。这是一个小问题

来自一个方案书(我已经离开了这个帖子,因为这个

帖子包含太多用于Scheme新闻组的Python代码): http://groups.google.com/group/comp。 ...线程/线程/ ...


函数multiremberandco很难(对我而言)如果完成那个

little Scheme子集,但用Python很容易。它从给定的列表中收集两个

列表,最后它将通用的给定乐趣

函数应用于两个收集的列表并返回其结果:


def multiremberandco1(el,seq,fun):

l1,l2 = [],[]

for x in seq:

如果x == el:

l2.append(el)

else:

l1.append(el)

返回乐趣(l1,l2)



喜欢熊市,


你不能吗?还使用count而不是显式循环来获得类似的东西:


def multiremberandco1(el,seq,fun):

l1,l2 = [],[]

c = seq.count(e1)

l1 = [el] * c

l2 = [el] *(len(seq) - c)

返回乐趣(l1,l2)


我没有这本书所以可以'评论它的适用性,

但是你去了。


- Paddy。

是************ @ lycos.com 写道:


所以我试图为有趣的函数扫描创建两个iterables

seq一次,但到目前为止我还没有成功(要做的)它我试图使用增强型发生器使用两个协同程序,根据x == el的值将el发送到另一个或

到另一个,这次我不要显示

我的失败版本),你有什么建议吗?



如果你想让两个输出迭代器同时可用,我认为没有任何方法可以使用像list或

tee这样的东西。 。

假设输入迭代器中有17次出现el。唯一的

方法就是扫描整个迭代器。但现在所有

的元素必须在输出处再次可用。


Once in while I too have something to ask. This is a little problem
that comes from a Scheme Book (I have left this thread because this
post contains too much Python code for a Scheme newsgroup):
http://groups.google.com/group/comp....9f78eb4457d08/

The function multiremberandco is hard (for me still) if done in that
little Scheme subset, but it''s very easy with Python. It collects two
lists from a given one, at the end it applies the generic given fun
function to the two collected lists and returns its result:

def multiremberandco1(el, seq, fun):
l1, l2 = [], []
for x in seq:
if x == el:
l2.append(el)
else:
l1.append(el)
return fun(l1, l2)

data = [1, ''a'', 3, ''a'', 4, 5, 6, ''a'']
print multiremberandco1(''a'', data, lambda l1,l2: (len(l1), len(l2)))

More compact:

def multiremberandco2(el, seq, fun):
l1, l2 = [], []
for x in seq:
[l1, l2][x == el].append(el)
return fun(l1, l2)
A bit cleaner (but I don''t like it much):

def multiremberandco3(el, seq, fun):
l1, l2 = [], []
for x in seq:
(l2 if x == el else l1).append(el)
return fun(l1, l2)
For fun I have tried to make it lazy, if may be useful if seq is a
very long iterable. So I''ve used tee:

from itertools import ifilter, tee

def multiremberandco4(el, iseq, fun):
iseq1, iseq2 = tee(iseq)
iterable1 = ifilter(lambda x: x == el, iseq1)
iterable2 = ifilter(lambda x: x != el, iseq2)
return fun(iterable1, iterable2)

def leniter(seq):
count = 0
for el in seq:
count += 1
return count

idata = iter(data)
print multiremberandco4(''a'', idata, lambda l1,l2: (leniter(l1),
leniter(l2)))
But from the docs: >in general, if one iterator is going to use most
or all of the data before the other iterator, it is faster to use
list() instead of tee().<

So I have tried to create two iterables for the fun function scanning
seq once only, but I haven''t succed so far (to do it I have tried to
use two coroutines with the enhanced generators, sending el to one or
to the other according to the value of x == el, this time I don''t show
my failed versions), do you have suggestions?

(Note: in some situations it may be useful to create a "splitting"
function that given an iterable and a fitering predicate, returns two
lazy generators, of the items that satisfy the predicate and of the
items that don''t satisfy it, so this exercise isn''t totally useless).

Bye,
bearophile

解决方案

be************@lycos.com wrote:

Once in while I too have something to ask. This is a little problem
that comes from a Scheme Book (I have left this thread because this
post contains too much Python code for a Scheme newsgroup):
http://groups.google.com/group/comp....9f78eb4457d08/

The function multiremberandco is hard (for me still) if done in that
little Scheme subset, but it''s very easy with Python. It collects two
lists from a given one, at the end it applies the generic given fun
function to the two collected lists and returns its result:

def multiremberandco1(el, seq, fun):
l1, l2 = [], []
for x in seq:
if x == el:
l2.append(el)
else:
l1.append(el)
return fun(l1, l2)

data = [1, ''a'', 3, ''a'', 4, 5, 6, ''a'']
print multiremberandco1(''a'', data, lambda l1,l2: (len(l1), len(l2)))

More compact:

def multiremberandco2(el, seq, fun):
l1, l2 = [], []
for x in seq:
[l1, l2][x == el].append(el)
return fun(l1, l2)
A bit cleaner (but I don''t like it much):

def multiremberandco3(el, seq, fun):
l1, l2 = [], []
for x in seq:
(l2 if x == el else l1).append(el)
return fun(l1, l2)
For fun I have tried to make it lazy, if may be useful if seq is a
very long iterable. So I''ve used tee:

from itertools import ifilter, tee

def multiremberandco4(el, iseq, fun):
iseq1, iseq2 = tee(iseq)
iterable1 = ifilter(lambda x: x == el, iseq1)
iterable2 = ifilter(lambda x: x != el, iseq2)
return fun(iterable1, iterable2)

def leniter(seq):
count = 0
for el in seq:
count += 1
return count

idata = iter(data)
print multiremberandco4(''a'', idata, lambda l1,l2: (leniter(l1),
leniter(l2)))
But from the docs: >in general, if one iterator is going to use most
or all of the data before the other iterator, it is faster to use
list() instead of tee().<

So I have tried to create two iterables for the fun function scanning
seq once only, but I haven''t succed so far (to do it I have tried to
use two coroutines with the enhanced generators, sending el to one or
to the other according to the value of x == el, this time I don''t show
my failed versions), do you have suggestions?

(Note: in some situations it may be useful to create a "splitting"
function that given an iterable and a fitering predicate, returns two
lazy generators, of the items that satisfy the predicate and of the
items that don''t satisfy it, so this exercise isn''t totally useless).

Bye,
bearophile


I think it might be provable that two iterators are required for this
exercise. In this example, for example, leniter() will be called on one
list and then the other. How will it get an accurate count of one list
without iterating through the other?

I.e.:

data = [1, ''a'', 3, ''a'', 4, 5, 6, ''a'']
^
To count this ''a'', it must have gone through the
numbers, so the leniter() can not act independently
on the lists using a single iterator.

James


On Apr 17, 1:14 am, bearophileH...@lycos.com wrote:

Once in while I too have something to ask. This is a little problem
that comes from a Scheme Book (I have left this thread because this
post contains too much Python code for a Scheme newsgroup):http://groups.google.com/group/comp....thread/thread/...

The function multiremberandco is hard (for me still) if done in that
little Scheme subset, but it''s very easy with Python. It collects two
lists from a given one, at the end it applies the generic given fun
function to the two collected lists and returns its result:

def multiremberandco1(el, seq, fun):
l1, l2 = [], []
for x in seq:
if x == el:
l2.append(el)
else:
l1.append(el)
return fun(l1, l2)

Hi bearophile,

Couldn''t you also use count rather than the explicit loop to
get something like:

def multiremberandco1(el, seq, fun):
l1, l2 = [], []
c = seq.count(e1)
l1 = [el] * c
l2 = [el] * (len(seq) - c)
return fun(l1, l2)

I don''t have the book so can''t comment on its suitability,
but there you go.

- Paddy.


be************@lycos.com writes:

So I have tried to create two iterables for the fun function scanning
seq once only, but I haven''t succed so far (to do it I have tried to
use two coroutines with the enhanced generators, sending el to one or
to the other according to the value of x == el, this time I don''t show
my failed versions), do you have suggestions?

I think there is not any way short of using something like list or
tee, if you want both output iterators to be available simultaneously.
Say there are 17 occurrences of el in the input iterator. The only
way to know this is to scan through the whole iterator. But now all
of its elements have to be available again at the output.


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

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