Want to reduce steps of an operation with dictionaries

James Stroud jstroud at mbi.ucla.edu
Tue Oct 24 23:36:02 EDT 2006


Ben Finney wrote:
> pretoriano_2001 at hotmail.com writes:
> 
> 
>>I have next dictionaries:
>>a={'a':0, 'b':1, 'c':2, 'd':3}
>>b={'a':0, 'c':1, 'd':2, 'e':3}
>>I want to put in a new dictionary named c all the keys that are in b
>>and re-sequence the values.
> 
> 
> They never had a sequence, so you can't "re-sequence" them.
> 
> You have not, perhaps, worked your way through the Python
> tutorial. It's quite comprehensive and will give you a good grounding
> in the basic concepts.
> 
>     <URL:http://docs.python.org/tut/>
> 

The values in the example are sequentially ordered wrt the keys, which 
is perhaps what the OP intends.


c = dict(((k,v) for (v,k) in enumerate(x for x in sorted(a.keys()) if 
b.has_key(x))))

But, yes, there is decided ambiguity in his description of the problem.

James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list