multiple sequences to filter()

Alex Martelli alex at magenta.com
Thu Aug 24 11:38:15 EDT 2000


"Peter Schneider-Kamp" <nowonder at nowonder.de> wrote in message
news:39A54ACE.2D3DE878 at nowonder.de...
> Ben Wolfson wrote:
> >
> > list1, list2 = filter(lambda x,y: x==y, list1, list2)
> >
> > Which, of course, does not work, even though it seems like a natural
thing
> > to do.
>
> If you can explain the meaning of that in general terms
> (for l1, l2, ... = filter(boolfunc, l1, l2, ...)
> it shouldn't be a problem to implement.
>
> I doubt that you can come up with one, though.

What about...:

def filterzip(boolfunc, *sequences):
    tuples=apply(map,(None,)+sequences)
    result=[]
    for atuple in tuples:
        if apply(boolfunc,atuple):
            result.append(atuple)
    return result

I.e.: filterzip requires the first argument (boolfunc)
to accept N arguments, when multifilter is called with
N+1 arguments (boolfunc, followed by N sequences).  It
zips the sequences, but only returns those tuples for
which boolfunc returns a true result.

Not sure if it should start with a map(None, ...), or
a zip(...) [truncating to the shortest length, as zip;
or extending-with-None, as map]; probably the latter,
but I don't have a 2.0 installed to try it with zip,
so...:-).

More concisely, but, I think, equivalently:

def filterzip(bf, *sequences):
    return filter(lambda x,bf=bf: apply(bf,x),
        apply(map,(None,)+sequences))


Now,

def extendedfilter(bf, *seqs):
    return apply(map,(None,)+tuple(filterzip(bf,*seqs)))

may be an approximation to what Ben Wolfson was asking
for.  It does seem to apply to his example case:

list1=['a','b','e','l','d','o','n']
list2=['e','b','a','r','d','o','n']
list3, list4 = extendedfilter(lambda x,y: x==y, list1, list2)

binds both list3 and list4 to ('b', 'd', 'o', 'n').

And of course it works as an extension to filter:

extendedfilter(lambda x: x in 'aeiou','abeldon')
[('a', 'e', 'o')]

Well, ok, there are some issues with the KINDs of
sequences being returned -- tuples vs lists vs
strings -- and sequence-of-just-one-element vs that
element itself.  I guess those could be fixed.

Also unclear what extendedfilter should do when
the first argument is None.  This could be considered
an error if there is more than one other argument,
I guess.


_Should_ filter behave in such wise when called with
more than two arguments?  No opinion.  I guess it IS
a natural extension, but, would it get enough mileage
to be worth the bother?  Again, no opinion.


Alex






More information about the Python-list mailing list