Python ディクショナリ
- 前のページ Python セット
- 次のページ Python If Else
辞書(Dictionary)
辞書は無序で可変、インデックスを持つ集合です。Pythonでは、辞書は花括弧で書かれ、キーと値を持ちます。
实例
辞書の作成および表示:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } print(thisdict)
項のアクセス
辞書の項にアクセスするには、括弧内にキー名を参照します:
实例
"model" キーの値を取得します:
x = thisdict["model"]
さらに、 get()
の方法も同じ結果が得られます:
实例
"model" キーの値を取得します:
x = thisdict.get("model")
値の変更
特定の項の値を変更するには、そのキー名を参照できます:
实例
"year" を 2019 に変更します:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict["year"] = 2019
辞書を巡回する
辞書を巡回するには、 for
辞書を巡回します。
辞書を巡回する際には、返り値は辞書のキーですが、値を返す方法もあります。
实例
辞書内のすべてのキー名を個別に表示します:
for x in thisdict: print(x)
实例
辞書内のすべての値を個別に表示します:
for x in thisdict: print(thisdict[x])
实例
以下のように使用できます: values()
関数は辞書の値を返します:
for x in thisdict.values(): print(x)
实例
items() 関数を使用してキーと値を遍历します:
for x, y in thisdict.items(): print(x, y)
キーの存在を確認する
辞書に指定されたキーが存在するか確認するには、 in
キーワード:
实例
辞書に "model" が存在するか確認します:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } if "model" in thisdict: print("Yes, 'model' is one of the keys in the thisdict dictionary")
項の追加
新しいインデックスキーを使用して値を割り当てることで、項を辞書に追加できます:
实例
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict["color"] = "red" print(thisdict)
項の削除
辞書から項を削除するためのいくつかの方法があります:
实例
pop() メソッドは指定されたキー名を持つ項を削除します:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict.pop("model") print(thisdict)
实例
popitem()
最後に挿入されたプロジェクトを削除する方法(バージョン3.7以前では、ランダムなプロジェクトを削除):
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict.popitem() print(thisdict)
实例
del
关键字删除具有指定键名的项目:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } del thisdict["model"] print(thisdict)
实例
del
关键字也可以完全删除字典:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } del thisdict print(thisdict) #this 会导致错误,因为 "thisdict" 不再存在。
实例
clear()
关键字清空字典:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } thisdict.clear() print(thisdict)
复制字典
您不能通过键入 dict2 = dict1
来复制字典,因为:dict2
只是对 dict1
的引用,而 dict1
中的更改也将自动在 dict2
中进行。
有一些方法可以进行复制,一种方法是使用内建的字典方法 copy()
。
实例
使用 copy()
方法来复制字典:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } mydict = thisdict.copy() print(mydict)
制作副本的另一种方法是使用内建方法 dict()
。
实例
使用 dict()
方法创建字典的副本:
thisdict = { "brand": "Porsche", "model": "911", "year": 1963 } mydict = dict(thisdict) print(mydict)
嵌套字典
词典也可以包含许多词典,这被称为嵌套词典。
实例
创建包含三个字典的字典:
myfamily = { "child1" : { "name" : "Phoebe Adele", "year" : 2002 }, "child2" : { "name" : "Jennifer Katharine", "year" : 1996 }, "child3" : { "name" : "Rory John", "year" : 1999 } }
或者,如果您想嵌套三个已经作为字典存在的字典:
实例
创建三个字典,然后创建一个包含其他三个字典的字典:
child1 = { "name" : "Phoebe Adele", "year" : 2002 } child2 = { "name" : "Jennifer Katharine", "year" : 1996 } child3 = { "name" : "Rory John", "year" : 1999 } myfamily = { "child1" : child1, "child2" : child2, "child3" : child3 }
dict() 构造函数
也可以使用 dict()
构造函数创建新的字典:
实例
thisdict = dict(brand="Porsche", model="911", year=1963) # 注意:关键字不是字符串字面量 # 注意:赋值时使用的是等号而非冒号 print(thisdict)
辞書メソッド
Python は辞書上で使用できる内建メソッドのセットを提供します。
メソッド | 説明 |
---|---|
clear() | 辞書内のすべての要素を削除します |
copy() | 辞書のコピーを返します |
fromkeys() | 指定されたキーと値を持つ辞書を返します |
get() | 指定されたキーの値を返します |
items() | 各キー値ペアを含むタプルのリストを返します |
keys() | 辞書のキーを含むリストを返します |
pop() | 指定されたキーを持つ要素を削除します |
popitem() | 最後に挿入されたキー値ペアを削除します |
setdefault() | 指定されたキーの値を返します。そのキーが存在しない場合、指定された値を持つキーを追加します |
update() | 指定されたキー値ペアを使用して辞書を更新します |
values() | 辞書内のすべての値のリストを返します |
- 前のページ Python セット
- 次のページ Python If Else