Python dictionary

Dictionary type

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 term

key and value

The key is on the left and the value is on the right.

dict = {"key1" : value, "key2": value2 }

sort

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?".

Prior knowledge

>>> 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]

Use lambda function

#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)]

Two-dimensional dictionary

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

Python dictionary
[Python] dictionary
Python dictionary
[Python] Memo dictionary
[Python] Dictionary (hash)
Python basics: dictionary
Python Basic Course (7 Dictionary)
Python
Python3 List / dictionary memo
Python Dictionary Beginner's Guide
Create a dictionary in Python
Python> dictionary / collections> defaultdict () / Counter ()
Avoid KeyError in python dictionary
Python> dictionary> get ()> optional value
kafka python
Python basics ⑤
python + lottery 6
Built-in python
Python comprehension
Python technique
Studying python
Notes on Python and dictionary types
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python function ①
Python basics
Python memo
ufo-> python (3)
Expansion by argument of python dictionary
Python comprehension
Replace dictionary value with Python> update ()
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
Python for super beginners Python # dictionary type 1 for super beginners
python tips
Installing Python 3.4.3.
Try python
Python memo
Python iterative
Python algorithm
Dictionary type 2
[Python] Variables
Python functions
Python sys.intern ()
Python decimals
python underscore
Python summary
Start python
Dictionary type 1
[Python] Sort
Note: Python
Python basics ③
Python basics
[Scraping] Python scraping