Monday, June 14, 2010

rename keys in a dictionary

Programmer Question

i want to rename the keys of a dictionary are which are ints, and i need them to be ints with leading zeros's so that they sort correctly.



for example my keys are like:



'1','101','11'


and i need them to be:



'001','101','011'


this is what im doing now, but i know there is a better way



tmpDict = {}
for oldKey in aDict:
tmpDict['%04d'%int(oldKey)] = aDict[oldKey]
newDict = tmpDict


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails