LoginSignup
55
55

More than 5 years have passed since last update.

Python3系の基礎文法(辞書)

Last updated at Posted at 2017-07-08

概要

O'Reilly Japan の「入門 Python 3」を参考に、Python3系の基礎文法を勉強します。
同じようにPythonを勉強したいと思ってる方の参考になれば幸いです。

辞書(dict)

  • 辞書はミュータブルであるため、キー/値要素を追加、削除、追加することができる。
  • 他の言語では、ハッシュマップや連想配列と呼ぶ

辞書の作成

>>> # 空配列の作成 []
>>> target = {}
>>> target
{}

## 辞書作成
>>> dict_sample = {
...     "key1" : "value1",
...     "key2" : "value2",
...     "key3" : "value3",
... }
>>> dict_sample
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

dict() による辞書への変換

2要素のシーケンスを含むものであれば変換が可能。

>>> # lol(2要素のリストのリスト) を変換
>>> lol = [['key1','val1'], ['key2','val2'], ['key3','val3']]
>>> dict(lol)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

>>> # lot(2要素のタプルのリスト) を変換
>>> lot = [('key1','val1'), ('key2','val2'), ('key3','val3')]
>>> dict(lot)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

>>> # tol(2要素のリストのタプル)を変換
>>> tol = (['key1','val1'], ['key2','val2'], ['key3','val3'])
>>> dict(tol)
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}

>>> # los(2字の文字列のリスト)を変換
>>> los = ['a1','b2','c3']
>>> dict(los)
{'a': '1', 'b': '2', 'c': '3'}

>>> # tos(2字の文字列のタプル)を変換
>>> tos = ('a1','b2','c3')
>>> dict(tos)
{'a': '1', 'b': '2', 'c': '3'}

[key] による要素の取得、追加、変更

>>> dict1 = {
...     "key1" : "value1",
...     "key2" : "value2",
... }

>>> # 要素の取得
>>> dict1['key1']
'value1'

>>> # 要素の追加
>>> dict1['key3'] = 'value3'
>>> dict1
{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

>>> # 要素の変更
>>> dict1['key3'] = 'VALUE3'
>>> dict1
{'key1': 'value1', 'key2': 'value2', 'key3': 'VALUE3'}

update()による辞書の結合

>>> dict1 = {
...     "key1" : "value1",
...     "key2" : "value2",
... }
>>> dict2 = {
...     "key2" : "val2",
...     "key3" : "val3",
...     "key4" : "val4",
... }

>>> # 辞書の結合(同じキー値は更新される)
>>> dict1.update(dict2)
>>> dict1
{'key1': 'value1', 'key2': 'val2', 'key3': 'val3', 'key4': 'val4'}

delによる要素の削除

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }
>>> del dict1["key2"]
>>> dict1
{'key1': 'val1', 'key3': 'val3'}

clear()による全ての要素の削除

>>> # clear()を使う場合
>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }
>>> dict1.clear()
>>> dict1
{}

>>> # 空辞書の代入を使う場合
>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }
>>> dict1 = {}
>>> dict1
{}

inによるキー値の有無確認

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
... }
>>> "key1" in dict1
True
>>> "key3" in dict1
False

keys()、values()、items() による用途に応じたリストへの変換

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }

>>> # dict_keysオブジェクト(イテラブルなキーのビュー)で返却される
>>> dict1.keys()
dict_keys(['key1', 'key2', 'key3'])

>>> # keys()で全てのキーをlistで取得
>>> list( dict1.keys() )
['key1', 'key2', 'key3']

>>> # values()で全ての値をlistで取得
>>> list( dict1.values() )
['val1', 'val2', 'val3']

>>> # items()で全てのキー/値のペアをlistで取得
>>> list( dict1.items() )
[('key1', 'val1'), ('key2', 'val2'), ('key3', 'val3')]

copy()による辞書のコピー

>>> dict1 = {
...     "key1" : "val1",
...     "key2" : "val2",
...     "key3" : "val3",
... }
>>> dict2 = dict1.copy()
>>> dict2
{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}
55
55
3

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