OrderedDictは辞書のサブクラスで、要素/項目が追加された順番を保持します。
OrderedDictは、要素の挿入順序を保持します。
デフォルトのdictでは順序が保存されず、任意の順序で反復される結果となります。
最初に、OrderedDictライブラリモジュールを使用するために、collectionsモジュールをインポートする必要があります。
import collections
from collections import OrderedDict
|
この記事もチェック:Pythonで辞書(dict)の要素を削除する方法を解説する
Python OrderedDict の機能性
- OrderedDict オブジェクトの作成
- OrderedDict へのアイテムの追加
- OrderedDict からのアイテムの入れ替え
- OrderedDict からのアイテムの削除
- Key-Value Change
- move_to_end()関数
- OrderedDict の pop アイテム
- Reverse iteration
- OrderedDictのEquality Test
1. OrderedDict オブジェクトの生成
オブジェクトの生成には、OrderedDict()
関数を用いる。
from collections import OrderedDict
my_input = { 'Pune' : 'Maharashtra' , 'Ahemadnagar' : 'Gujarat' , 'Orrisa' : 'Bhubhaneshwar' }
# creating ordered dict from dict ordered_input = OrderedDict(my_input)
print (ordered_input)
|
結果を出力すると、以下の様になります。
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
2. OrderedDictへの項目追加
from collections import OrderedDict
my_input = { 'Pune' : 'Maharashtra' , 'Ahemadnagar' : 'Gujarat' , 'Orrisa' : 'Bhubhaneshwar' }
# creating ordered dict from dict ordered_input = OrderedDict(my_input)
#print(ordered_input) print ( "Adding item to OrderedDict...." )
ordered_input[ 'Hyderabad' ] = 'Karnataka'
print (ordered_input)
|
結果を出力すると、以下の様になります。
Adding item to OrderedDict....
OrderedDict([('Pune', 'Maharashtra'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar'), ('Hyderabad', 'Karnataka')])
3. OrderedDictの項目の入れ替え
from collections import OrderedDict
my_input = { 'Pune' : 'Maharashtra' , 'Ahemadnagar' : 'Gujarat' , 'Orrisa' : 'Bhubhaneshwar' }
# creating ordered dict from dict ordered_input = OrderedDict(my_input)
#print(ordered_input) print ( "Replacing item from OrderedDict...." )
ordered_input[ 'Pune' ] = 'Satara'
print (ordered_input)
|
結果を出力すると、以下の様になります。
Adding items to OrderedDict....
OrderedDict([('Pune', 'Satara'), ('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
4. OrderedDictからの項目削除
from collections import OrderedDict
my_input = { 'Pune' : 'Maharashtra' , 'Ahemadnagar' : 'Gujarat' , 'Orrisa' : 'Bhubhaneshwar' }
# creating ordered dict from dict ordered_input = OrderedDict(my_input)
#print(ordered_input) print ( 'Removal of item from OrderedDict....' )
ordered_input.pop( 'Pune' )
print (ordered_input)
|
結果は、以下の通りになります。
Removal of item from OrderedDict....
OrderedDict([('Ahemadnagar', 'Gujarat'), ('Orrisa', 'Bhubhaneshwar')])
5. OrderedDictにおけるキーと値の変化
OrderedDictにおいて、あるキーに対応する値が変更されても、そのキーの位置やインデックスが変更されることはない。
from collections import OrderedDict
my_input = { 'Pune' : 'Maharashtra' , 'Ahemadnagar' : 'Gujarat' , 'Orrisa' : 'Bhubhaneshwar' }
# creating ordered dict from dict print ( 'Before the change.....' )
ordered_input = OrderedDict(my_input)
print (ordered_input)
print ( 'After the change.....' )
ordered_input[ 'Pune' ] = 'Kiara'
print (ordered_input)
|
結果を出力すると、以下の様になります。
出力:“`
Before the change…..
OrderedDict([(‘Pune’, ‘Maharashtra’), (‘Ahemadnagar’, ‘Gujarat’), (‘Orrisa’, ‘Bhubhaneshwar’)])
After the change…..
OrderedDict([(‘Pune’, ‘Kiara’), (‘Ahemadnagar’, ‘Gujarat’), (‘Orrisa’, ‘Bhubhaneshwar’)])
---
### 6. move_to_end() 関数
move_to_end()`関数は、特定のKey-Valueペアをdictの末尾に移動させる。
<div class="wp-block-syntaxhighlighter-code"><div><div class="syntaxhighlighter nogutter python" id="highlighter_955209"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="python keyword">from</code> <code class="python plain">collections </code><code class="python keyword">import</code> <code class="python plain">OrderedDict</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="python plain">my_input </code><code class="python keyword">=</code> <code class="python plain">{</code><code class="python string">'Pune'</code><code class="python plain">: </code><code class="python string">'Maharashtra'</code><code class="python plain">, </code><code class="python string">'Ahemadnagar'</code><code class="python plain">: </code><code class="python string">'Gujarat'</code><code class="python plain">, </code><code class="python string">'Orrisa'</code><code class="python plain">: </code><code class="python string">'Bhubhaneshwar'</code><code class="python plain">}</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="python comments"># creating ordered dict from dict</code></div><div class="line number7 index6 alt2"><code class="python functions">print</code><code class="python plain">(</code><code class="python string">'Before using the move_to_end().....'</code><code class="python plain">)</code></div><div class="line number8 index7 alt1"><code class="python plain">ordered_input </code><code class="python keyword">=</code> <code class="python plain">OrderedDict(my_input)</code></div><div class="line number9 index8 alt2"><code class="python functions">print</code><code class="python plain">(ordered_input)</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"><code class="python functions">print</code><code class="python plain">(</code><code class="python string">'After using the move_to_end().....'</code><code class="python plain">)</code></div><div class="line number12 index11 alt1"><code class="python plain">ordered_input.move_to_end(</code><code class="python string">'Pune'</code><code class="python plain">)</code></div><div class="line number13 index12 alt2"><code class="python functions">print</code><code class="python plain">(ordered_input)</code></div></div></td></tr></tbody></table></div></div></div>
出力
Before using the move_to_end()…..
OrderedDict([(‘Pune’, ‘Maharashtra’), (‘Ahemadnagar’, ‘Gujarat’), (‘Orrisa’, ‘Bhubhaneshwar’)])
After using the move_to_end()…..
OrderedDict([(‘Ahemadnagar’, ‘Gujarat’), (‘Orrisa’, ‘Bhubhaneshwar’), (‘Pune’, ‘Maharashtra’)])
---
### 7. オーダーディクト popitem
この関数はポップアウトし、最後の要素を出力として返します。
<div class="wp-block-syntaxhighlighter-code"><div><div class="syntaxhighlighter nogutter python" id="highlighter_75208"><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="python keyword">from</code> <code class="python plain">collections </code><code class="python keyword">import</code> <code class="python plain">OrderedDict</code></div><div class="line number2 index1 alt1"> </div><div class="line number3 index2 alt2"> </div><div class="line number4 index3 alt1"><code class="python plain">my_input </code><code class="python keyword">=</code> <code class="python plain">{</code><code class="python string">'Pune'</code><code class="python plain">: </code><code class="python string">'Maharashtra'</code><code class="python plain">, </code><code class="python string">'Ahemadnagar'</code><code class="python plain">: </code><code class="python string">'Gujarat'</code><code class="python plain">, </code><code class="python string">'Orrisa'</code><code class="python plain">: </code><code class="python string">'Bhubhaneshwar'</code><code class="python plain">}</code></div><div class="line number5 index4 alt2"> </div><div class="line number6 index5 alt1"><code class="python comments"># creating ordered dict from dict</code></div><div class="line number7 index6 alt2"><code class="python functions">print</code><code class="python plain">(</code><code class="python string">'Original input dict.....'</code><code class="python plain">)</code></div><div class="line number8 index7 alt1"><code class="python plain">ordered_input </code><code class="python keyword">=</code> <code class="python plain">OrderedDict(my_input)</code></div><div class="line number9 index8 alt2"><code class="python functions">print</code><code class="python plain">(ordered_input)</code></div><div class="line number10 index9 alt1"> </div><div class="line number11 index10 alt2"> </div><div class="line number12 index11 alt1"><code class="python plain">result </code><code class="python keyword">=</code> <code class="python plain">ordered_input.popitem(</code><code class="python color1">True</code><code class="python plain">)</code></div><div class="line number13 index12 alt2"><code class="python functions">print</code><code class="python plain">(</code><code class="python string">'The popped item is: '</code><code class="python plain">)</code></div><div class="line number14 index13 alt1"><code class="python functions">print</code><code class="python plain">(result)</code></div><div class="line number15 index14 alt2"> </div><div class="line number16 index15 alt1"><code class="python functions">print</code><code class="python plain">(ordered_input)</code></div></div></td></tr></tbody></table></div></div></div>
出力。
Original input dict…..
OrderedDict([(‘Pune’, ‘Maharashtra’), (‘Ahemadnagar’, ‘Gujarat’), (‘Orrisa’, ‘Bhubhaneshwar’)])
The popped item is:
(‘Orrisa’, ‘Bhubhaneshwar’)
OrderedDict([(‘Pune’, ‘Maharashtra’), (‘Ahemadnagar’, ‘Gujarat’)])
“`
8. reverse iteration
Reverse iteration
False tag
Fake code
—FALSE CODE
9. OrderedDict Equality Test
False Tags
False Tag
False Code
—False code
おわりに
この記事では、通常の辞書とOrderedDictの違いを理解し、OrderedDictが提供する機能を見てきました。