開発環境
- 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)、Test Your Knowledge: Part II Exercises、5.(Dictionary keys)を解いてみる。
その他参考書籍
5.(Dictionary keys)
入出力結果(Terminal)
$ python3
Python 3.3.4 (default, Feb 10 2014, 22:06:52)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> d={}
>>> d=[1]='a'
File "<stdin>", line 1
SyntaxError: can't assign to literal
>>> d[1]='a'
>>> d[2]='b'
>>> d # {1:'a', 2:'b'}
{1: 'a', 2: 'b'}
>>> d[(1, 2, 3)]='c' # 不変性を持つオブジェクトならキーに使える
>>> d
{1: 'a', 2: 'b', (1, 2, 3): 'c'}
>>> d[[1,2]]='d' # リストは可変性を持つオブジェクトなので、キーに使えない エラーTraceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> d
{1: 'a', 2: 'b', (1, 2, 3): 'c'}
>>> quit()
$
0 コメント:
コメントを投稿