Pythonクイズ(辞書の非破壊的操作)の解答
以下のようにdictコンストラクタが辞書自身とキーワード引数の両方をとれることを利用。
>>> d1 = {'a':1, 'b':2, 'c':3} >>> d2 = {'c':4, 'd':5, 'e':6} >>> print dict(d1, **d2) {'a': 1, 'c': 4, 'b': 2, 'e': 6, 'd': 5} >>> d1 {'a': 1, 'c': 3, 'b': 2} >>> d2 {'c': 4, 'e': 6, 'd': 5}
sumimさんとmopemopeさんに以下のご回答を頂きました。ありがとうございました。
>>> print dict(d1.items() + d2.items())
以下は今回の問題と関係ないがおまけ。
- Sucksシリーズ (http://shinh.skr.jp/h/)
- PythonSucks (http://shinh.skr.jp/h/?PythonSucks)
>>> len("hoge") 4 >>> len("hoge",) 4 >>> "hoge", ('hoge',) >>> len(("hoge",)) 1
Pythonは微妙にクセがある。