There is a large dictionary like {'вася': 4, 'сон': 3, 'пол': 4} and so on.

It is necessary to obtain an object containing all the keys with the same values, provided that the number of these values ​​is not known.

Desirable sorted in ascending order.

Is it possible to create conditionally named variables in python? I would like, for example, something like this:

 q = get ALL from {} where val == (foreach range(1, {}.__len__())) 

and then create a variable by type

 create str(list({}.values.sortbyinc)) 

unload all dictionary keys with a value, for example, 1 and so on for all found values.

    2 answers 2

     from collections import defaultdict, OrderedDict dct = {'вася': 4, 'сон': 3, 'пол': 4} defdct = defaultdict(list) for key, value in dct.items(): defdct[value].append(key) print(defdct) # Напечатает: # defaultdict(<class 'list'>, {3: ['сон'], 4: ['пол', 'вася']}) # По сути это обычный словарь, # из которого можно по числовому ключу получить список слов # А теперь из него вот так можно получить упорядоченный словарь, # отсортированный по возрастанию числового ключа: sorted_dct = OrderedDict(sorted(defdct.items())) 

    You can also sort by other attributes if you use the key argument (examples of using sorted with the key argument are easy to google)

       dict_1 = {'вася': 4, 'сон': 3, 'пол': 4} new_object = [(x,dict_1[x]) for x in dict_1.keys() if dict_1[x] == 4] print(new_object) 

      Result:

       [('вася', 4), ('пол', 4)] 
      • >>> if dict_1 [x] == 4] And if the values ​​in the dictionary are 20,000? and we do not know in advance the melee, or the larger step - Badalamenti
      • Didn't quite understand you - Pavel Durmanov
      • The script analyzes the text using Counter, which produces a dictionary, the keys in which are words, and the value = the number of these words in the text. - Badalamenti
      • What you wrote in the comment and the question is different ... - Pavel Durmanov
      • Well, why is it different? The question was about the possibility of sampling from the dictionary all the keys with the same value (1, then 2, etc.) and the sequential storage of such samples in a variable (or buffer), with the conditional naming: "1", "2", etc. - Badalamenti