Python syntax in Lisp and Scheme

David Eppstein eppstein at ics.uci.edu
Mon Oct 6 14:59:22 EDT 2003


In article <blsbpf$i9n$1 at newsreader2.netcologne.de>,
 Pascal Costanza <costanza at web.de> wrote:

> I don't know a lot about Python, so here is a question. Is something 
> along the following lines possible in Python?
> 
> (with-collectors (collect-pos collect-neg)
>    (do-file-lines (l some-file-name)
>      (if (some-property l)
>        (collect-pos l)
>        (collect-neg l))))
> 
> 
> I actually needed something like this in some of my code...

Not using simple generators afaik.  The easiest way would probably be to 
append into two lists:

    collect_pos = []
    collect_neg = []
    for l in some_file_name:
        if some_property(l):
            collect_pos.append(l)
        else:
            collect_neg.append(l)

If you needed to do this a lot of times you could encapsulate it into a 
function of some sort:

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)

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list