Python核心编程课后习题-第七章(未完)

2016-11-09  本文已影响37人  无愠无殇

第七章--GitHub地址

7-1

7–1. 字典方法。哪个字典方法可以用来把两个字典合并到一起?

dict1.update(dict2)

7-2

7–2. 字典的键。我们知道字典的值可以是任意的 Python 对象,那字典的
键又如何呢?请试着将除数字和字符串以外的其他不同类型的对象作为字典的键,
看一看,哪些类型可以,哪些不行?
对那些不能作字典的键的对象类型,你认为是什么原因呢?
>>> dic = {(1,2):2}
>>> dic = {[1,2]:2}
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> dic = {(1,[1,2]):2}
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> dic = {[1,(1,2)]:2}
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> dic = {{1:2}:2}
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: unhashable type: 'dict'

可变类型不能作为字典的键。

上一篇下一篇

猜你喜欢

热点阅读