Nested string substitutions

Lulu of the Lotus-Eaters mertz at gnosis.cx
Fri Dec 20 00:42:10 EST 2002


I think I am being daft here.  But I cannot think of an elegant way to
perform a nested expansion of a set of substitution patterns.

For example, let's say I have a dictionary of patterns:

    subs = {'foo':'bar baz',
            'bar':'bat bam ber',
            'baz':'zat zam zer',
            'bam':'yat yam yer',
            'yam':'spam and eggs'}

I want to "fully expand" the substitution dictionary, getting:

    subs = {'foo':'bat yat spam and eggs yer ber zat zam zer',
            'bar':'bat yat spam and eggs yer ber',
            'baz':'zat zam zer',
            'bam':'yat spam and eggs yer',
            'yam':'spam and eggs'}

I can assume that there is no mutual recursion of substitutions to make
thing blow up.

I know I can do this with a while loop and a "nothing_left_to_do"
monitor variable, and some nested '.items()' loops.  But that looks like
a mess.  It really seems like I should be able to do this with one or
two lines of an amazing listcomp or map().

Yours, Lulu...

--
 mertz@   _/_/_/_/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY:_/_/_/_/ v i
gnosis  _/_/                    Postmodern Enterprises         _/_/  s r
.cx    _/_/  MAKERS OF CHAOS....                              _/_/   i u
      _/_/_/_/_/ LOOK FOR IT IN A NEIGHBORHOOD NEAR YOU_/_/_/_/_/    g s





More information about the Python-list mailing list