python version
Python 3.7.3 (default, Mar 28 2020, 17:59:31)
[Clang 9.1.0 (clang-902.0.39.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
The key is on the left and the value is on the right.
dict = {"key1" : value, "key2": value2 }
As an example, consider a dictionary with a one-to-one correspondence between country names and population.
>>> population
{'India': 1380004385, 'China': 1439323776, 'Pakistan': 220892340, 'UnitedStates': 331002651, 'Indonesia': 273523615}
When the top 5 countries in the population are arranged appropriately, it is necessary to sort by key or value, such as "I want to sort in alphabetical order" and "Which country has the top population?".
>>> population.items()
dict_items([('India', 1380004385), ('China', 1439323776), ('Pakistan', 220892340), ('UnitedStates', 331002651), ('Indonesia', 273523615)])
sorted There are two types of sorting, whether to sort by key or value.
#Sort keys alphabetically
>>> sorted(population)
['China', 'India', 'Indonesia', 'Pakistan', 'UnitedStates']
#descending order
>>> sorted(population, reverse=True)
['UnitedStates', 'Pakistan', 'Indonesia', 'India', 'China']
#Sort alphabetically and return each list
>>> sorted(population.items())
[('China', 1439323776), ('India', 1380004385), ('Indonesia', 273523615), ('Pakistan', 220892340), ('UnitedStates', 331002651)]
#Sort by value only (when sorting in descending order, reverse described later=Use True option)
>>> sorted(population.values())
[220892340, 273523615, 331002651, 1380004385, 1439323776]
#Sort the dictionary by key
>>> sorted(population.items(), key = lambda x : x[0])
[('China', 1439323776), ('India', 1380004385), ('Indonesia', 273523615), ('Pakistan', 220892340), ('UnitedStates', 331002651)]
#Sort by dictionary value
>>> sorted(population.items(), key = lambda x : x[1])
[('Pakistan', 220892340), ('Indonesia', 273523615), ('UnitedStates', 331002651), ('India', 1380004385), ('China', 1439323776)]
Next, consider a type in which the value of the dictionary is a list (in this example, the first component is the total population and the second component is the annual rate of change).
>>> population
{'India': [1380004385, 0.99], 'China': [1439323776, 0.39], 'Pakistan': [220892340, 2.0], 'UnitedStates': [331002651, 0.59], 'Indonesia': [273523615, 1.07]}
#Sort by dictionary key
>>> sorted(population.items(), key = lambda x : x[0] )
[('China', [1439323776, 0.39]), ('India', [1380004385, 0.99]), ('Indonesia', [273523615, 1.07]), ('Pakistan', [220892340, 2.0]), ('UnitedStates', [331002651, 0.59])]
#Sort by dictionary value first component
>>> sorted(population.items(), key = lambda x: x[1][0], reverse=True )
[('China', [1439323776, 0.39]), ('India', [1380004385, 0.99]), ('UnitedStates', [331002651, 0.59]), ('Indonesia', [273523615, 1.07]), ('Pakistan', [220892340, 2.0])]
## -->It can be seen that the population is the largest in the order of China, India, the United States, Indonesia, and Pakistan.
#Sort by dictionary value second component
>>> sorted(population.items(), key = lambda x: x[1][1], reverse=True )
[('Pakistan', [220892340, 2.0]), ('Indonesia', [273523615, 1.07]), ('India', [1380004385, 0.99]), ('UnitedStates', [331002651, 0.59]), ('China', [1439323776, 0.39])]
## -->It can be seen that the annual growth rate is highest in the order of Pakistan, Indonesia, India, the United States, and China.
Lambda expressions x [1] [0]
and x [1] [1]
may be a little confusing.
>>> population.items()
dict_items([('India', [1380004385, 0.99]), ('China', [1439323776, 0.39]), ('Pakistan', [220892340, 2.0]), ('UnitedStates', [331002651, 0.59]), ('Indonesia', [273523615, 1.07])])
As you can see, it says that the value of the dictionary prepared by x [1]
is expressed, and then the 0th value of value is used by x [1] [0]
. It is.
Recommended Posts