day06python 哈希 字典集合嵌套

1.hash算法

  • hash算法 :内存中将值进行hash算法得到一个数值存储在内存中,查找也会按照算法进行查找,使用hash算法 执行效率高相对于list的索引查找
  • (字典,集合):使用的是hash查找,因为只有(不可变的数据类型)才能被hash,而列表/字典/集合 (可以改变的) ==>不能放在(集合,字典的键)中

2.集合嵌套,字典键值

1.列表/字典/集合 (可以改变的) ->不能放在(集合,字典的键)中
info = {1, 2, 3, 4, True, "国风", None, (1, 2, 3)}
print(info)
#{1, 2, 3, 4, None, (1, 2, 3), '国风'}
#True被忽略掉  True被转换成1  因为集合中元素不能重复所以被过滤掉

#加入列表后报错
info = {1, 2, 3, 4, True, "国风", None, (1, 2, 3),["a","b","c"]}
print(info)
#TypeError: unhashable type: 'list'

#加入集合后报错
info = {1, 2, 3, 4, True, "国风", None, (1, 2, 3),{33,44,55}}
print(info)
#TypeError: unhashable type: 'set'

#加入字典后报错
info = {1, 2, 3, 4, True, "国风", None, (1, 2, 3),{"name":'Gao',"age":12}}
print(info)
#TypeError: unhashable type: 'dict'

猜你喜欢

转载自www.cnblogs.com/koukouku/p/10679184.html