本文介绍了将字典转换为编号格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下多个字典,它们是python脚本的输入:
I have the following multi-dictionaries which are inputs to the python script:
{ 123 {'all_uppercase': 0, '#': 1, 'longword': 5, '@': 1} }
{ 486 {'all_uppercase': 0, '#': 0, 'longword': 0, '@': 0} }
我必须转换成编号格式,即123将是1,486将2,等等。
I have to convert into numbering format i.e. 123 will be 1, 486 will be 2 and so on.
对于'all_uppercase':0,'#':1,'stop_words':10,'longword' 5,'@':1
all_uppercase将为1,stop_words将为2,longword将为3,@将为4.
For 'all_uppercase': 0, '#': 1, 'stop_words': 10,'longword': 5, '@': 1
all_uppercase will be 1, stop_words will be 2, longword will be 3, @ will be 4.
所以应该打印的最终输出应该如下所示:
So the final output that should be printed should look like this:
{ 1 {'1': 0, '2': 1, '3': 5, '4': 1} }
{ 2 {'1': 0, '2': 0, '3': 0, '4': 0} }
以下是我的代码:
inner_dict = {}
count =0
def analyze_tweets():
for line in sys.stdin:
d = ast.literal_eval(line)
for k,v in d.items():
inner_dict = dicts.setdefault(k, {})
for i in inner_dict.items()
推荐答案
如果您的字典是
d = {'one':1, 'two':2, 'three':3}
那么你必须这样做:
dict((key,val) for key,val in enumerate(sorted(d))
这将关联号码到您的密钥。
This will associate numbers to your keys.
这篇关于将字典转换为编号格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!