multirember&co

Paddy paddy3118 at googlemail.com
Tue Apr 17 00:37:31 EDT 2007


On Apr 17, 1:14 am, bearophileH... at 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.lang.scheme/browse_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.




More information about the Python-list mailing list