2.8 可变、不可变数据类型和hash
本节重点
可变与不可变类型
>>> l = [1,2,3,4]
>>> id(l)
4392665160
>>> l[1] = 1.5
>>> l
[1, 1.5, 3, 4]
>>> id(l)
4392665160
hash

可以被hash的内容
Last updated
>>> l = [1,2,3,4]
>>> id(l)
4392665160
>>> l[1] = 1.5
>>> l
[1, 1.5, 3, 4]
>>> id(l)
4392665160

Last updated
>>> a = 1
>>> id(a)
4297537952
>>> a+=1
>>> id(a)
4297537984#例1
>>> s = 'hello'
>>> s[1] = 'a'
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
s[1] = 'a'
TypeError: 'str' object does not support item assignment
#例2
>>> s = 'hello'
>>> id(s)
4392917064
>>> s += ' world'
>>> s
'hello world'
>>> id(s)
4393419504>>> t = (1,2,3,4)
>>> t[1] = 1.5
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
t[1] = 1.5
TypeError: 'tuple' object does not support item assignment张三 13980593357
李四 15828662334
王老五 13409821234
[[‘张三’,13980593357][‘李四’,15828662334][‘王老五’,13409821234]]假如对上述的联系人信息进行存储时,采用的Hash函数为:姓名的每个字的拼音开头大写字母的ASCII码之和。因此
address(张三)=ASCII(Z)+ASCII(S)=90+83=173;
address(李四)=ASCII(L)+ASCII(S)=76+83=159;
address(王老五)=ASCII(W)+ASCII(L)+ASCII(W)=87+76+87=250;>>> hash("张三")
6480394008723176318
>>> hash("李四")
-114706925611844552
>>> hash("王老五")
3250319002057530081