dict和set的key不可变
来源:互联网 发布:哪个软件可以搜高数题 编辑:程序博客网 时间:2024/05/29 18:28
dict和set的key是不可变的 :
- tuple虽然是不可变对象,但是只有不含list的tuple才能做key,包含有list的tuple是“可变的”
- list是可以改变的,所以肯定是不能做key的**
>>> a=(1,2,3)>>> b=(1,[2,3])>>> dict1={a:'A'}>>> dict1[a]'A'>>> dict2={b:'B'}Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unhashable type: 'list'>>>
这里a是不含list的tuple 作为key不会报错
而b会报错
试试把list放入set,看看是否会报错。
>>> s=set([1,2,3,2])>>> sset([1, 2, 3])>>> list=[3,4]>>> s.add(list)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: unhashable type: 'list'
结论:是一定会报错的 因为set中的key也要求不变 list是可变的
阅读全文
0 0
- dict和set的key不可变
- python学习(五)之dict+set+不可变对象
- python中的key-value结构的dict和set的key值选择
- python的list,tuple,dist,set和不可变对象
- Scala集合,序列(可变和不可变List),List各种函数的使用,不可变Set和可变Set,Map
- dict和set的使用
- dict和set的使用
- Python笔记1——list,tuple,dict,set,不可变对象
- python dict.get()和dict['key']的使用
- python里面的dict和set
- 使用dict和set
- 4.dict和set
- Dict和Set类型
- dict和set
- dict(字典)和set
- 使用dict和set
- 使用dict和set
- 使用dict和set
- Course Schedule
- 抽象类继承问题
- 创建继承类Perint的类Student
- 快速幂讲解
- NIO
- dict和set的key不可变
- hibernate配置文件
- OpenGL_8:着色器之间的变量传递
- linux进程及系统服务的控制
- 栈简单实现
- UVM Practice Chap2
- 数论+gcd
- Java中String类转换为Date日期显示
- 撤销修改