0

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

I have to convert into numbering format i.e. 123 will be 1, 486 will be 2 and so on.

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

Here is my code:

 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()
4

1 回答 1

1

如果你的字典是,

 d = {'one':1, 'two':2, 'three':3}

那么你必须这样做:

 dict((key,val) for key,val in enumerate(sorted(d))

这会将数字与您的密钥相关联。

于 2013-11-05T00:32:44.130 回答