String formatting with two dictionaries?

Peter Hansen peter at engcorp.com
Mon Oct 8 09:59:17 EDT 2001


Carsten Gaebler wrote:
> 
> Given a string
> 
> s = "%(foo)s %(spam)s"
> 
> and two dictionaries
> 
> d1 = {"foo": "bar"}
> d2 = {"spam": "eggs"}
> 
> how would you apply d1 and d2 to s without modifying or explicitly
> copying one of the dicts? I mean, I don't like to
> 
> d3 = dictionary(d1)
> d3.update(d2)
> s = s % d3

Split the string into parts which don't contain names from
both dictionaries...

(I doubt you'll "like" to do that either.  I think you're
asking for something that can't be done in a way that would
satisfy you.)

If you seriously want another answer, you might need to 
specify the requirements further.  Do the dictionaries
ever contain duplicate keys?  Why don't you want to 
do the copy and update? (you don't say, and any solution
put forth runs the risk of violating your unexpressed 
restrictions and having you not "like" it..)

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list