一、相关操作
字典不支持加法、乘法和比较大小的运算
举例:
print({
'a': 10} + {
'b': 20})
print({
'a': 100} > {
'b': 10})
运行结果:
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
TypeError: '>' not supported between instances of 'dict' and 'dict'
判断数据本身是否相等: ==,!=
print({
'a': 10, 'b': 20} == {
'b': 20, 'a': 10})
True
is - 判断两个数据的地址是否相等
举例:
a = {
'a': 10}
b = {
'a': 10}
c = a
print(a == b)
print(a is b)
print(a is c)
运行结果:
True
False
True
in 和 not in
key in 字典 - 判断字典中是否有指定的key
举例:
dict1 = {
'a': 10, 'b': 20, 'c': 30}
print(10 in dict1)
print('b' in dict1)
运行结果:
False
True
相关函数: len\dict
len(字典) - 获取字典中键值对的个数
举例:
dict1 = {
'a': 10, 'b': 20, 'c':