original
字典键都是整数。如何使用更短的方法将所有整数键转换为字符串?如何将嵌套字典键转换为字符串?
original = {1:{},2:{101:"OneZeroOne",202:"TwoZeroTwo"}}
result = {}
for key in original:
if not key in result:
result[str(key)]={}
for i, value in original[key].items():
result[str(key)][str(i)] = value
print result
打印:
{'1': {}, '2': {'202': 'TwoZeroTwo', '101': 'OneZeroOne'}}
多少级你需要递归? –
'''......较短的方法......''' - 执行时间或更少的代码行数? – wwii