開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のIV部(関数)のまとめ演習5.(ディクショナリのコピー)を解いてみる。
その他参考書籍
5.(ディクショナリのコピー)
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
def copyDict(d):
res = {}
for k, v in d.items():
res[k] = v
return res
d = {'a':1,'b':2,'c':3,'d':4,'e':5}
d1 = copyDict(d)
print(d, d1, sep="\n")
# ディクショナリにはスライシングは使えない
try:
d1 = d[:]
except Exception as err:
print(type(err), err, err.args)
入出力結果(Terminal)
$ ./sample.py
{'d': 4, 'e': 5, 'a': 1, 'b': 2, 'c': 3}
{'d': 4, 'e': 5, 'a': 1, 'b': 2, 'c': 3}
<class 'TypeError'> unhashable type: 'slice' ("unhashable type: 'slice'",)
$
0 コメント:
コメントを投稿