回答編集履歴
1
追記
test
CHANGED
@@ -99,3 +99,47 @@
|
|
99
99
|
}
|
100
100
|
|
101
101
|
```
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
コメントを受けて
|
106
|
+
|
107
|
+
---
|
108
|
+
|
109
|
+
ヒントだけ。
|
110
|
+
|
111
|
+
```Python
|
112
|
+
|
113
|
+
>>> from collections import OrderedDict
|
114
|
+
|
115
|
+
>>>
|
116
|
+
|
117
|
+
>>> hoge = {'foo': 4, 'bar': 6, 'foobar': 7}
|
118
|
+
|
119
|
+
>>> sorted_hoge = OrderedDict()
|
120
|
+
|
121
|
+
>>> for k, v in sorted(hoge.items(), key=lambda x: x[1], reverse=True):
|
122
|
+
|
123
|
+
... sorted_hoge[k] = v
|
124
|
+
|
125
|
+
...
|
126
|
+
|
127
|
+
>>> print(sorted_hoge)
|
128
|
+
|
129
|
+
OrderedDict([('foobar', 7), ('bar', 6), ('foo', 4)])
|
130
|
+
|
131
|
+
```
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
dictのビューに比べ、OrderedDictのビューは一定の順序を保証します。
|
136
|
+
|
137
|
+
平たく言うと、`.items()`や`.keys()`、`.values()`による値が挿入順になるということです。
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
適宜リファレンスを見て、理解を深めるようにしてください。
|
142
|
+
|
143
|
+
・ [Python 標準ライブラリ » 組み込み関数 » sorted](https://docs.python.jp/3/library/functions.html#sorted)
|
144
|
+
|
145
|
+
・ [Python 標準ライブラリ » データ型 » collections » OrderedDict](https://docs.python.jp/3/library/collections.html#collections.OrderedDict)
|