0

GUYS i reversed a dictionary...but how can i sort ascending the dict exactly like this:

 {'precise': ['accurate', 'exact'],
 'clever': ['astute', 'smart'],
 'talented': ['smart'],
 'bright': ['smart'],
 'exact': ['accurate'],
 'smart': ['astute']}

MY Solution don't return that i want:

def reverse_dictionary(input_dict):
   d = {}
   s=input_dict
   for key, value in s.items():
       value=" ".join(value)
       value=value.lower()
       value=value.split(' ')
       key=key.lower()
       for i in sorted(value):
           d.setdefault(i, []).append(key)
   return d
input_dict=({'Accurate': ['exact', 'precise'],
            'exact': ['precise'],
            'astute': ['Smart', 'clever'],
            'smart': ['clever', 'bright', 'talented']})
print(reverse_dictionary(input_dict))

it returns:

{'exact': ['accurate'],
 'precise': ['accurate', 'exact'], 
 'clever': ['astute', 'smart'],
 'smart': ['astute'],
 'bright': ['smart'],
 'talented': ['smart']}

but it's not true!

samad
  • 1
  • 2
    dictionaries can't be sorted. Look into [OrderedDict](https://docs.python.org/2/library/collections.html#collections.OrderedDict) – foxyblue Aug 11 '17 at 09:27
  • 1
    I propose renaming _SO_ to _SO (dictionaries in Python cannot be ordered)_ – Ma0 Aug 11 '17 at 09:30
  • but i dont wanna sort it by key...some keys has 2 or 3 items in list as value! i wanna sort it by value ...which has more item in list – samad Aug 11 '17 at 09:32
  • This looks like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What do you ultimately want to accomplish? – molbdnilo Aug 11 '17 at 09:34
  • @samad It **does not** matter what you want to _sort by_, `dictionaries` in Python **cannot** be sorted. – Ma0 Aug 11 '17 at 09:35

0 Answers0