Python sorted dict by list value

Как отсортировать словарь по значению в Python

Словарь в Python — это набор элементов, в которых данные хранятся в виде пар ключ-значение. В Python 3.7 и более поздних версиях словари сортируются по порядку вставки элементов. В более ранних версиях они были неупорядоченными.

Давайте посмотрим, как мы можем отсортировать словарь на основе содержащихся в нем значений.

Сортировка словаря с помощью цикла for

Мы можем отсортировать словарь с помощью цикла for . Сначала мы используем функцию sorted() для упорядочивания значений словаря. Затем мы перебираем отсортированные значения, находя ключи для каждого значения. Мы добавляем эти пары ключ-значение в отсортированном порядке в новый словарь.

Примечание. Сортировка не позволяет изменять порядок словаря на месте. Записываем упорядоченные пары в совершенно новый пустой словарь.

dict1 = sorted_values = sorted(dict1.values()) # Sort the values sorted_dict = <> for i in sorted_values: for k in dict1.keys(): if dict1[k] == i: sorted_dict[k] = dict1[k] break print(sorted_dict) 

Если вы запустите это с помощью интерпретатора Python, вы увидите:

Теперь, когда мы увидели, как выполнять сортировку с помощью циклов, давайте рассмотрим более популярную альтернативу, использующую эту функцию sorted() .

Читайте также:  Java jmx management javax

Сортировка словаря с использование функция sorted()

Ранее мы использовали эту функцию для сортировки значений массива. При сортировке словаря, мы можем передать еще один аргумент к функции: sorted(dict1, key=dict1.get) .

Вот функция key , которая вызывается для каждого элемента перед сравнением значений для сортировки. Метод get() по словарю объектов возвращает значение для ключа словаря.

Выражение sorted(dict1, key=dict1.get) возвращает список ключей, значения сортируются по порядку. Оттуда мы можем создать новый отсортированный словарь:

dict1 = sorted_dict = <> sorted_keys = sorted(dict1, key=dict1.get) # [1, 3, 2] for w in sorted_keys: sorted_dict[w] = dict1[w] print(sorted_dict) #

Использование функции sorted() сократило объем кода, который нам приходилось писать при использовании циклов for . Однако мы можем дополнительно комбинировать функцию sorted() с itemgetter() для более лаконичного решения сортировки словарей по значениям.

Сортировка словаря с помощью модуля operator и itemgetter()

Модуль operator включает в себя функцию itemgetter() . Эта функция возвращает вызываемый объект, который возвращает элемент из объекта.

Например, давайте использовать itemgetter() для создания вызываемого объекта, который возвращает значение любого словаря с ключом, который 2 :

import operator dict1 = get_item_with_key_2 = operator.itemgetter(2) print(get_item_with_key_2(dict1)) # 9

Каждый словарь имеет доступ к методу items() . Эта функция возвращает пары «ключ-значение» словаря в виде списка кортежей. Мы можем отсортировать список кортежей, используя функцию itemgetter() для извлечения второго значения кортежа, то есть значения ключей в словаре.

После сортировки мы можем создать словарь на основе этих значений:

import operator dict1 = sorted_tuples = sorted(dict1.items(), key=operator.itemgetter(1)) print(sorted_tuples) # [(1, 1), (3, 4), (2, 9)] sorted_dict = print(sorted_dict) #

С гораздо меньшими усилиями у нас есть словарь, отсортированный по значениям!

Поскольку аргумент key принимает любую функцию, мы можем использовать лямбда-функции для возврата значений словаря, чтобы их можно было отсортировать. Посмотрим как.

Сортировка словаря с помощью лямбда-функции

Лямбда-функции в Python являются анонимными или безымянными функциями. Мы можем использовать lamba-функции для получения значения элемента словаря без необходимости импорта модуля operator для itemgetter() .

Давайте отсортируем словарь по значениям, используя лямбда-функцию в аргументе key :

dict1 = sorted_tuples = sorted(dict1.items(), key=lambda item: item[1]) print(sorted_tuples) # [(1, 1), (3, 4), (2, 9)] sorted_dict = print(sorted_dict) #

Обратите внимание, что методы, которые мы обсуждали до сих пор, работают только с Python 3.7 и новее. Давайте посмотрим, что мы можем сделать для более ранних версий Python.

Возврат нового словаря с отсортированными значениями

После сортировки словаря по значениям, чтобы сохранить отсортированный словарь в версиях Python до 3.7, вы должны использовать OrderedDict — доступный в модуле collections . Эти объекты представляют собой словари, сохраняющие порядок вставки.

Вот пример сортировки и использования OrderedDict :

import operator from collections import OrderedDict dict1 = sorted_tuples = sorted(dict1.items(), key=operator.itemgetter(1)) print(sorted_tuples) # [(1, 1), (3, 4), (2, 9)] sorted_dict = OrderedDict() for k, v in sorted_tuples: sorted_dict[k] = v print(sorted_dict) #

Вывод

В этом руководстве показано, как можно отсортировать словарь на основе его значений. Сначала мы отсортировали словарь, используя два цикла for. Затем мы улучшили нашу сортировку с помощью функции sorted() . Мы также видели, что функция itemgetter() модуля operator может сделать наше решение более лаконичным.

Наконец, мы адаптировали наше решение для работы с версиями Python ниже 3.7.

Варианты функции sorted() — самые популярные и надежные для сортировки словаря по значениям.

Источник

Sort Dictionary by Value in Python – How to Sort a Dict

Kolade Chris

Kolade Chris

Sort Dictionary by Value in Python – How to Sort a Dict

In Python, a dictionary is a fat structure that is unordered by default. So, sometimes, you’ll want to sort dictionaries by key or value to make queries easier.

The problem is that sorting a dictionary by value is never a straightforward thing to do. That’s because Python doesn’t have an inbuilt method to do it.

However, I figured out a way to sort dictionaries by value, and that’s what I’m going to show you how to do in this article.

What We’ll Cover

How to Sort Data with the sorted() Method

The sorted() method sorts iterable data such as lists, tuples, and dictionaries. But it sorts by key only.

The sorted() method puts the sorted items in a list. That’s another problem we have to solve, because we want the sorted dictionary to remain a dictionary.

For instance, sorted() arranged the list below in alphabetical order:

persons = ['Chris', 'Amber', 'David', 'El-dorado', 'Brad', 'Folake'] sortedPersons = sorted(persons) print(sortedPersons) # Output: ['Amber', 'Brad', 'Chris', 'David', 'El-dorado', 'Folake'] 

And the sorted() method sorts the numbers in the tuple below in ascending order:

numbers = (14, 3, 1, 4, 2, 9, 8, 10, 13, 12) sortedNumbers = sorted(numbers) print(sortedNumbers) # Output: [1, 2, 3, 4, 8, 9, 10, 12, 13, 14] 

If you use the sorted() method with a dictionary, only the keys will be returned and as usual, it will be in a list:

my_dict = < 'num6': 6, 'num3': 3, 'num2': 2, 'num4': 4, 'num1': 1, 'num5': 5>sortedDict = sorted(my_dict) print(sortedDict) # ['num1', 'num2', 'num3', 'num4', 'num5', 'num6'] 

This is not the behavior you want. You want the dictionary to be sorted by value and remain a dictionary. That’s what I’m going to show you next.

How the sorted() Method Works

To sort a dictionary, we are still going to use the sorted function, but in a more complicated way. Don’t worry, I will explain everything you need to know.

Since we are still going to use the sorted() method, then it’s time to explain the sorted() method in detail.

Parameters of the sorted() Method

The sorted() method can accept up to 3 parameters:

  • iterable – the data to iterate over. It could be a tuple, list, or dictionary.
  • key – an optional value, the function that helps you to perform a custom sort operation.
  • reverse – another optional value. It helps you arrange the sorted data in ascending or descending order

If you guess it right, the key parameter is what we’ll pass into the sorted() method to get the dictionary sorted by value.

Now, it’s time to sort our dictionary by value and make sure it remains a dictionary.

How to Sort a Dictionary with the sorted() Method

To correctly sort a dictionary by value with the sorted() method, you will have to do the following:

  • pass the dictionary to the sorted() method as the first value
  • use the items() method on the dictionary to retrieve its keys and values
  • write a lambda function to get the values retrieved with the item() method
footballers_goals = sorted_footballers_by_goals = sorted(footballers_goals.items(), key=lambda x:x[1]) print(sorted_footballers_by_goals) 

As I said earlier, we have to get those values of the dictionary so we can sort the dictionary by values. That’s why you can see 1 in the lambda function.

1 represents the indexes of the values. The keys are 0. Remember that a programmer starts counting from 0, not 1.

With that code above, I got the result below:

# [('Cruyff', 104), ('Eusebio', 120), ('Messi', 125), ('Ronaldo', 132), ('Pele', 150)] 

Here’s the full code so you don’t get confused:

footballers_goals = sorted_footballers_by_goals = sorted(footballers_goals.items(), key=lambda x:x[1]) print(sorted_footballers_by_goals) # [('Cruyff', 104), ('Eusebio', 120), ('Messi', 125), ('Ronaldo', 132), ('Pele', 150)] 

You can see the dictionary has been sorted by values in ascending order. You can also sort it in descending order. But we’ll look at that later because we still have a problem with the result we got.

The problem is that the dictionary is not a dictionary anymore. The individual keys and values were put in a tuple and further condensed into a list. Remember that whatever you get as the result of the sorted() method is put in a list.

We’ve been able to sort the items in the dictionary by value. What’s left is converting it back to a dictionary.

How to Convert the Resulting List to a Dictionary

To convert the resulting list to a dictionary, you don’t need to write another complicated function or a loop. You just need to pass the variable saving the resulting list into the dict() method.

converted_dict = dict(sorted_footballers_by_goals) print(converted_dict) # Output:

Remember we saved the sorted dictionary in the variable named sorted_footballers_by_goals , so it’s the variable we have to pass to dict() .

The full code looks like this:

footballers_goals = sorted_footballers_by_goals = sorted(footballers_goals.items(), key=lambda x:x[1]) converted_dict = dict(sorted_footballers_by_goals) print(converted_dict) # Output:

That’s it! We’ve been able to sort the items in the dictionary and convert them back to a dictionary. We’ve just had our cake and ate it as well!

How to Sort the Dictionary by Value in Ascending or Descending Order

Remember the sorted() method accepts a third value called reverse .

reverse with a value of True will arrange the sorted dictionary in descending order.

footballers_goals = sorted_footballers_by_goals = sorted(footballers_goals.items(), key=lambda x:x[1], reverse=True) converted_dict = dict(sorted_footballers_by_goals) print(converted_dict) # Output:

You can see the output is reversed because we passed reverse=True to the sorted() method.

If you don’t set reverse at all or you set its value to false, the dictionary will be arranged in ascending order. That’s the default.

Conclusion

Congratulations. You can now sort a dictionary by value despite not having a built-in method or function to use in Python.

However, there’s something that raised my curiosity when I was preparing to write this article. Remember we were able to use sorted() directly on a dictionary. This got us a list as the result, though we only got the keys and not the values.

What if we convert that list to a dictionary with the dict() method? Do you think we can get the desired result? Let’s see:

my_dict = < 'num6': 6, 'num3': 3, 'num2': 2, 'num4': 4, 'num1': 1, 'num5': 5>sortedDict = sorted(my_dict) converted_dict = dict(sortedDict) print(converted_dict) """ Output: dict_by_value.py Traceback (most recent call last): File "sort_dict_by_value.py", line 17, in converted_dict = dict(sortedDict) ValueError: dictionary update sequence element #0 has length 4; 2 is required """ 

We got an error! That’s because if you want to create a dictionary from a list, you have to use dictionary comprehension. And if you use dictionary comprehension for this type of data, you’d have to specify one value for all the entries. That would defy the purpose of sorting a dictionary by value, so it’s not what we want.

If you want to learn more about dictionary comprehension, you should read this article.

Источник

Оцените статью