Want to reduce steps of an operation with dictionaries
James Stroud
jstroud at mbi.ucla.edu
Tue Oct 24 23:28:29 EDT 2006
More information about the Python-list mailing list
Tue Oct 24 23:28:29 EDT 2006
- Previous message (by thread): Want to reduce steps of an operation with dictionaries
- Next message (by thread): Want to reduce steps of an operation with dictionaries
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
pretoriano_2001 at hotmail.com wrote: > Hello: > 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. The result I want is: > c={'a':0, 'c':1, 'd':2} > How can I do this with one line of instruction? > > I attempted the next but the output is not the expected: > c=dict([(k,v) for v,k in enumerate(a) if b.has_key(k)]) > erroneously (for me) gets: > {'a': 0, 'c': 2, 'd': 3} > > Thanks for your help. > I think you have the right idea if I understand what you want: c = dict(((k,v) for (v,k) in enumerate(x for x in a if b.has_key(x)))) -- James Stroud UCLA-DOE Institute for Genomics and Proteomics Box 951570 Los Angeles, CA 90095 http://www.jamesstroud.com/
- Previous message (by thread): Want to reduce steps of an operation with dictionaries
- Next message (by thread): Want to reduce steps of an operation with dictionaries
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list