简体   繁体   中英

Converting a dictionary into numbering format

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

If your dictionary is,

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

then you must do this:

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

This will associate numbers to your keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM