開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART II.(Types and Operations)、CHAPTER 4(Introducing Python Object Types)、Test Your Knowledge: Quiz 5を解いてみる。
その他参考書籍
Test Your Knowledge: Quiz 5
mappingはkey/valueのペアの集合。keyは一意的。pythonではディクショナリがmappingに相当する。
コード(BBEdit)
sample.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
# 順序は定義した場合と同様になるとは限らない
d = {'a':1, 'b':2, 'c':3, 'd':4}
print(d)
d['a'] = 10
print(d)
# keyが一意的になってないとどうなるか確認
d1 = {'a':1, 'b':2, 'a':10}
d2 = {'a':10, 'b':2, 'a':1}
print(d1)
print(d2)
# 結果から推測すると、一番右のが優先されて上書きされるみたい
入出力結果(Terminal)
$ ./sample.py
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
{'a': 10, 'c': 3, 'b': 2, 'd': 4}
{'a': 10, 'b': 2}
{'a': 1, 'b': 2}
$
0 コメント:
コメントを投稿