Want to reduce steps of an operation with dictionaries

Gabriel Genellina gagsl-py at yahoo.com.ar
Tue Oct 24 22:37:33 EDT 2006


At Tuesday 24/10/2006 21:29, pretoriano_2001 at hotmail.com wrote:

>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?

Why "one line"? Choosing the right data structure is far more 
important than squeezing the code to fit on one single line.

I'm not sure if I understand you correctly - or maybe you're 
confusing yourself.
If you're going to "re-sequence" the values, the original values are 
not relevant at all; so maybe you need a list instead of a dict.

Perhaps this is what you want (something like swapping keys<->values):

a = ['a', 'b', 'c', 'd']
b = ['a', 'c', 'd', 'e']
c = [x for x in a if x in b]
c == ['a', 'c', 'd']
c[0] == 'a'
c[1] == 'c'
c[2] == 'd'


-- 
Gabriel Genellina
Softlab SRL 

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar



More information about the Python-list mailing list