LoginSignup
1
1

More than 3 years have passed since last update.

Python辞書

Posted at

辞書型

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

用語

keyとvalue

左側にあるのがkeyで、右側にあるのがvalue。

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

並び替え

例として、国名と人口を一対一対応させた辞書を考える。

>>> population
{'India': 1380004385, 'China': 1439323776, 'Pakistan': 220892340, 'UnitedStates': 331002651, 'Indonesia': 273523615}

人口トップ5の国を適当に並べたときに、「アルファベット順に並べ替えたい」「人口トップはどの国だろう?」という、keyやvalueでソートしたいという作業が必要になる。

予備知識

>>> population.items()
dict_items([('India', 1380004385), ('China', 1439323776), ('Pakistan', 220892340), ('UnitedStates', 331002651), ('Indonesia', 273523615)])

sorted

並び替えには2種類あり、keyで並び替えるか、valueで並び替えるか。

# keyをアルファベット順で並べ替える
>>> sorted(population)
['China', 'India', 'Indonesia', 'Pakistan', 'UnitedStates']

# 降順
>>> sorted(population, reverse=True)
['UnitedStates', 'Pakistan', 'Indonesia', 'India', 'China']

# アルファベット順で並べ替え、各リストを返す
>>> sorted(population.items())
[('China', 1439323776), ('India', 1380004385), ('Indonesia', 273523615), ('Pakistan', 220892340), ('UnitedStates', 331002651)]

# valueだけで並び替える(降順にするときは、後述のreverse=Trueオプションを使用)
>>> sorted(population.values())
[220892340, 273523615, 331002651, 1380004385, 1439323776]

ラムダ関数を用いる

# 辞書をkeyでソートする
>>> sorted(population.items(), key = lambda x : x[0])
[('China', 1439323776), ('India', 1380004385), ('Indonesia', 273523615), ('Pakistan', 220892340), ('UnitedStates', 331002651)]

# 辞書のvalueでソートする
>>> sorted(population.items(), key = lambda x : x[1])
[('Pakistan', 220892340), ('Indonesia', 273523615), ('UnitedStates', 331002651), ('India', 1380004385), ('China', 1439323776)]

二次元辞書

次に、辞書のvalueがさらにリストであるような型を考える(この例の場合、第一成分が総人口、第二成分は年変化率)。

>>> population
{'India': [1380004385, 0.99], 'China': [1439323776, 0.39], 'Pakistan': [220892340, 2.0], 'UnitedStates': [331002651, 0.59], 'Indonesia': [273523615, 1.07]}
# 辞書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])]

# 辞書value第一成分で並び替え
>>> 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])]
## --> 中国、インド、アメリカ、インドネシア、パキスタンの順に人口が多いことがわかる

# 辞書value第二成分で並び替え
>>> 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])]
## --> パキスタン、インドネシア、インド、アメリカ、中国の順に年増加率が高いことがわかる

ラムダ式のx[1][0]x[1][1]が少しややこしいかもしれない。

>>> population.items()
dict_items([('India', [1380004385, 0.99]), ('China', [1439323776, 0.39]), ('Pakistan', [220892340, 2.0]), ('UnitedStates', [331002651, 0.59]), ('Indonesia', [273523615, 1.07])])

を見ればわかるように、x[1]で用意した辞書のvalueを表現し、次にx[1][0]でvalueの第0番目の値を使うということを言っているのである。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1