美文网首页
Python核心编程课后习题-第七章(未完)

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

作者: 无愠无殇 | 来源:发表于2016-11-09 16:43 被阅读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'
    

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

    相关文章

      网友评论

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

          本文链接:https://www.haomeiwen.com/subject/ugrblttx.html