データ構造(辞書)
書きやすくしてプログラマの作業性とコードの信頼性を高めることを重視してデザインされた、汎用の高水準言語です。
homepage
# **データ構造(辞書)** ### **辞書** 辞書は、順不同であり、重複の無いリストを扱います。 Pythonの辞書は、中括弧で書かれており、キーを指定することができます。 * 例 辞書の作成: ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } print (thisdict) ``` ### **項目へのアクセス** 丸括弧の内側に、そのキー名を記入することで、辞書内の該当する項目にアクセスすることができます。 * 例 `model`キーの値を取得します: ``` x = thisdict["model"] ``` * `get()`という名前のメソッドもあります。これを使うと同じ結果が得られます。 ``` x = thisdict.get("model") ``` ### **変更値** キー名を指定して特定の項目の値を変更できます。 * 「year」を2019年に変更します。 ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["year"] = 2019 ``` ### **辞書をループする** `for`ループを使用して辞書をループすることができます。 辞書をループするとき、戻り値は辞書のキーですが、値を返すメソッドもあります。 * 辞書内のすべてのキー名を1つずつ表示します。 ``` for x in thisdict: print (x) ``` * 辞書内のすべての値を1つずつ表示します。 ``` 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": "Ford", "model": "Mustang", "year": 1964 } if "model" in thisdict: print("はい、`model` はthisdictのキーの1つです") ``` ### **辞書の長さ** 辞書内の項目の数を出力するには、`len()`を使用します。 * 例 ``` print(len(thisdict)) ``` ### **項目の追加** 新たなインデックスキーを使用して、それに値を割り当てることで、辞書に項目を追加できます。 ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict["color"] = "red" print(thisdict) ``` ### **アイテムを削除します** 辞書から項目を削除するには、以下の方法があります。 * 例1: `pop()`メソッドで指定されたキー名を持つ項目を削除する。 ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.pop("model") print (thisdict) ``` * 例2: `popitem()`で後に挿入された項目を削除する(3.7以前のバージョンでは、ランダムアイテムが代わりに削除される) ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.popitem() print (thisdict) ``` * 例3: `del`キーワードで指定したキー名の項目を削除する。 ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } del thisdict["model"] print (thisdict) ``` * 例4: `del`キーワードで完全に辞書を削除する ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } del thisdict print (thisdict) #"thisdict"はもう存在しないので、これはエラーを引き起こします。. ``` * 例5: `clear()`キーワードで辞書を空にする ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } thisdict.clear() print (thisdict) ``` ### **辞書をコピーする** * 辞書メソッド `copy()`を使って、辞書のコピーを作成する方法: ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = thisdict.copy() print(mydict) ``` * 組み込みのメソッド `dict()`によって辞書のコピーを作成する方法: ``` thisdict = { "brand": "Ford", "model": "Mustang", "year": 1964 } mydict = dict(thisdict) print(mydict) ``` ### **`dict()`コンストラクタ** `dict(`)コンストラクターを使って新しい辞書を作ることも可能です。 ``` thisdict = dict(brand="Ford", model="Mustang", year=1964) # note that keywords are not string literals # note the use of equals rather than colon for the assignment print(thisdict) ``` ## 辞書メソッド <table border="1"> <tr> <td>Method</td> <td>Description</td> </tr> <tr> <td>clear()</td> <td>辞書からすべての要素を削除します</td> </tr> <tr> <td>copy()</td> <td>辞書のコピーを返します</td> </tr> <tr> <td>fromkeys()</td> <td>指定されたキーと値を持つ辞書を返します</td> </tr> <tr> <td>get()</td> <td>指定されたキーの値を返します</td> </tr> <tr> <td>items()</td> <td>各キー値ペアのaタプルを含むリストを返します</td> </tr> <tr> <td>keys()</td> <td>辞書のキーを含むリストを返します</td> </tr> <tr> <td>pop()</td> <td>指定されたキーを持つ要素を削除します</td> </tr> <tr> <td>popitem()</td> <td>最後に挿入されたキーと値のペアを削除します</td> </tr> <tr> <td>setdefault()</td> <td>指定されたキーの値を返します。 キーが存在しない場合:指定された値でキーを挿入します</td> </tr> <tr> <td>update()</td> <td>指定されたキーと値のペアで辞書を更新します</td> </tr> <tr> <td>values()</td> <td>辞書内のすべての値のリストを返します</td> </tr> </table> > 練習 >> `get`メソッドを使用して、自動車 辞書の "model"キーの値を表示します。。 ``` car = { "brand": "Ford", "model": "Mustang", "year": 1964 } print(______) >>> answer: car.get("model")
content
戻る