Τροποποίηση όλων των τιμών σε λεξικό

Ο κώδικας ακολουθεί:

d = {'a':0, 'b':0, 'c':0, 'd':0}  #at the beginning, all the values are 0.
s = 'cbad'  #a string
indices = map(s.index, d.keys())  #get every key's index in s, i.e., a-2, b-1, c-0, d-3
#then set the values to keys' index
d = dict(zip(d.keys(), indices))  #this is how I do it, any better way?
print d  #{'a':2, 'c':0, 'b':1, 'd':3}

Κάποιος άλλος τρόπος να το κάνεις αυτό;

PS. ο παραπάνω κωδικός είναι απλός για να δείξω την ερώτησή μου.

5
задан jpp 6 December 2018 в 15:45
поделиться