Help: Creating condensed expressions
Michael Spencer
mahs at telcopartners.com
Fri Mar 24 15:57:03 EST 2006
bearophileHUGS at lycos.com wrote:
> Nevermind, I didn't understand the problem/question... Sorry.
>
> Bye,
> bearophile
>
Really? Your solution looks fine to me.
In any case, here's an alternative approach to the (based on the same
understanding of the problem as bearophile's, but with the additional
requirement that the input terms be sorted)
>>> from itertools import groupby
>>> from operator import itemgetter
>>> src_iter = ((i[:-1],i[-1]) for i in src.splitlines()) #src must be sorted
>>> grouped = groupby(src_iter, itemgetter(0))
>>> stemmed = ((stem, "".join(i[1] for i in values)) for stem, values in grouped)
>>> [(len(s[1])>1 and "%s[%s]" or "%s%s") % s for s in stemmed]
['apple[12]', 'apple3_SD', 'form[ABC]', 'kla_M[MB]', 'kca_MM']
>>>
Michael
More information about the Python-list
mailing list