Python syntax in Lisp and Scheme

David Mertz mertz at gnosis.cx
Tue Oct 7 14:13:39 EDT 2003


|> def posneg(filter,iter):
|>     results = ([],[])
|>     for x in iter:
|>         results[not filter(x)].append(x)
|>     return results
|> collect_pos,collect_neg = posneg(some_property, some_file_name)

Pascal Costanza <costanza at web.de> wrote previously:
|What about dealing with an arbitrary number of filters?

Easy enough:

    def categorize_exclusive(filters, iter):
        results = tuple([[] for _ in len(filters)])
        for x in iter:
            for n, filter in enumerate(filters):
                if filter(x):
                    results[n].append(x)
                    break
    return results

Or if you want to let things fall in multiple categories:

    def categorize_inclusive(filters, iter):
        results = tuple([[] for _ in len(filters)])
        for x in iter:
            for n, filter in enumerate(filters):
                if filter(x):
                    results[n].append(x)
    return results

Or if you want something to satisfy ALL the filters:

    def categorize_compose(filters, iter):
        results = tuple([[] for _ in len(filters)])
        for x in iter:
            results[compose(filters)(x)].append(x)
    return results

The implementation of 'compose()' is left as an exercise to readers :-).
Or you can buy my book, and read the first chapter.

Yours, David...

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons.  Intellectual
property is to the 21st century what the slave trade was to the 16th.
--
Buy Text Processing in Python: http://tinyurl.com/jskh






More information about the Python-list mailing list