A nifty algoritm for filtering out values

RajGopal Srinivasan raj at grserv.med.jhmi.edu
Tue Nov 21 23:34:49 EST 2000


With Python 2.0

x = ['comp.lang.python', 'comp.lang.lisp', 'alt.test', 'alt.fan.jwz',
                 'no.it.os.unix.linux.diverse'] * 300

y = [s.split('.', 1)[0] for s in x]

raj

"Morten W. Petersen" wrote:
> 
> Any suggestions as to how this is best achieved (speed-wise) ?
> 
> If there is a list, like so:
> 
>         ['comp.lang.python', 'comp.lang.lisp', 'alt.test', 'alt.fan.jwz',
>                 'no.it.os.unix.linux.diverse'] * 300
> 
> What's the best way of getting another object that shows only something like
> this:
> 
>         ['comp', 'alt', 'no']
> 
> ?
> 
> Thanks.
> 
> (Semantically sweet algorithms are also interesting... =)
> 
> -Morten



More information about the Python-list mailing list