辞書 編集

Pythonの辞書(Dictionary)は、任意個のキーとコレクションです。

コンストラクターとリテラル 編集

リストリテラル
print(f"""\
{ {}=}
{ dict()=}
{ dict(((1, 1),(2, 4),(3,9)))=}
{ dict((i,2**i) for i in range(5))=}
{ {i:2**i for i in range(5)}=}
{ {10,"ABC", 20, "XYZ"}=}
{ {1:"one", 2:"two",3:"three"}=}
{ {
    1: "壱",
    2: "弐",
    3: "参"
    }=}
""")
実行結果
{}={}
 dict()={}
 dict(((1, 1),(2, 4),(3,9)))={1: 1, 2: 4, 3: 9}
 dict((i,2**i) for i in range(5))={0: 1, 1: 2, 2: 4, 3: 8, 4: 16}
 {i:2**i for i in range(5)}={0: 1, 1: 2, 2: 4, 3: 8, 4: 16}
 {10,"ABC", 20, "XYZ"}={10, 'XYZ', 'ABC', 20}
 {1:"one", 2:"two",3:"three"}={1: 'one', 2: 'two', 3: 'three'}
 {
    1: "壱",
    2: "弐",
    3: "参"
    }={1: '壱', 2: '弐', 3: '参'}
空の辞書は、空の辞書リテラル {} あるいはコンストラクターである組込み関数dict()を引数無しで呼出し生成します。
dict()の引数は、キーと値をを要素とするコレクションを要素とするコレクションです(文章だとややこしいですが、dict(((1, 1),(2, 4),(3,9)))=={1: 1, 2: 4, 3: 9}です)。
dict()には、ジェネレーション式などのイテレーターを渡す事もできます(dict((i,2**i) for i in range(5))=={0: 1, 1: 2, 2: 4, 3: 8, 4: 16}
辞書にも内包表記があります({i:2**i for i in range(5)}=={0: 1, 1: 2, 2: 4, 3: 8, 4: 16})。
辞書リテラルは、キーと値を : で区切ったペアをカンマ , で区切り、全体を { } で囲みます(: を , と間違えると(エラーにならず)セット(集合)のリテラルになり、気が付き遅れがちです。逆に {} を空集合∅のつもりで書き、辞書であることに気づかないケースもあります)。

キーの重複 編集

Pythonでは、辞書のキーの重複は許されず、最後の値で上書きされます。 これは辞書リテラルにも当てはまり、同じキーの最も右の値がリテラル内のキーの値となります[1]

キーが重複した辞書リテラル
# 国語が重複している
dic = {"国語": 80, "氏名": "山田タロウ", "国語": 70 }

print(dic["国語"])
print(dic)
実行結果
70
{'国語': 70, '氏名': '山田タロウ'}
Pythinの辞書は順位を保存するので、重複したキーの効果の痕跡が順位に出ています。

辞書のフィールド一覧 編集

辞書のフィールド一覧
obj = dict()
for i in dir(obj):
    print(i, eval(f"type({obj}.{i})"))
実行結果
__class__ <class 'type'>
__contains__ <class 'builtin_function_or_method'>
__delattr__ <class 'method-wrapper'>
__delitem__ <class 'method-wrapper'>
__dir__ <class 'builtin_function_or_method'>
__doc__ <class 'str'>
__eq__ <class 'method-wrapper'>
__format__ <class 'builtin_function_or_method'>
__ge__ <class 'method-wrapper'>
__getattribute__ <class 'method-wrapper'>
__getitem__ <class 'builtin_function_or_method'>
__gt__ <class 'method-wrapper'>
__hash__ <class 'NoneType'>
__init__ <class 'method-wrapper'>
__init_subclass__ <class 'builtin_function_or_method'>
__iter__ <class 'method-wrapper'>
__le__ <class 'method-wrapper'>
__len__ <class 'method-wrapper'>
__lt__ <class 'method-wrapper'>
__ne__ <class 'method-wrapper'>
__new__ <class 'builtin_function_or_method'>
__reduce__ <class 'builtin_function_or_method'>
__reduce_ex__ <class 'builtin_function_or_method'>
__repr__ <class 'method-wrapper'>
__reversed__ <class 'builtin_function_or_method'>
__setattr__ <class 'method-wrapper'>
__setitem__ <class 'method-wrapper'>
__sizeof__ <class 'builtin_function_or_method'>
__str__ <class 'method-wrapper'>
__subclasshook__ <class 'builtin_function_or_method'>
clear <class 'builtin_function_or_method'>
copy <class 'builtin_function_or_method'>
fromkeys <class 'builtin_function_or_method'>
get <class 'builtin_function_or_method'>
items <class 'builtin_function_or_method'>
keys <class 'builtin_function_or_method'>
pop <class 'builtin_function_or_method'>
popitem <class 'builtin_function_or_method'>
setdefault <class 'builtin_function_or_method'>
update <class 'builtin_function_or_method'>
values <class 'builtin_function_or_method'>

脚註 編集

  1. ^ Python 3.10.6 Documentation » The Python Language Reference » 6. Expressions » 6.2.7. Dictionary displays”. 2022年8月10日閲覧。 “If a comma-separated sequence of key/datum pairs is given, they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum. This means that you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given.

    カンマで区切られた一連のキー/データペアが与えられると、それらは左から右へと評価されて辞書のエントリーを定義します。各キーオブジェクトは、対応するデータを格納する辞書のキーとして使用されます。つまり、key/datumリストで同じキーを複数回指定しても、そのキーに対する最終的な辞書の値は、最後に指定されたものになります。”