開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅱ.(Types and Operations)、Chapter 4.(Introducing Python Object Types)、Test Your Knowledge: Quiz 5.を解いてみる。
その他参考書籍
Test Your Knowledge: Quiz 5.
mapping
- dictionary
コード(BBEdit)
sample5.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*_
d1 = {'a':1, 'b':2, 'c':2, 'd':1}
print(d1)
d2 = {1:'a', 'b':2, 3:'c', 'd':4}
print(d2)
d3 = {(1,):'a', (2,):'b'}
print(d3)
# ?
try:
d4 = {'a':1, 'a':2}
print(d4)
except Exception as err:
print(type(err), err, err.args)
try:
# keys must be immutable
d5 = {[1]:'a'}
print(d5)
except Exception as err:
print(type(err), err, err.args)
入出力結果(Terminal, IPython)
$ ./sample5.py
{'d': 1, 'b': 2, 'c': 2, 'a': 1}
{'d': 4, 1: 'a', 3: 'c', 'b': 2}
{(2,): 'b', (1,): 'a'}
{'a': 2}
<class 'TypeError'> unhashable type: 'list' ("unhashable type: 'list'",)
$
0 コメント:
コメントを投稿