python中is和==的区别以及全字段取出key和value
a = 1 # a为int类型
b = 1 # b为int类型
print(a is b)
print(a == b)
print(id(a))
print(id(b))
a = 'china' # a为str类型
b = 'china' # b为str类型
print(a is b)
print(a == b)
print(id(a))
print(id(b))
a = (1, 2, 3) # a为tuple类型
b = (1, 2, 3) # b为tuple类型
print(a is b)
print(a == b)
print(id(a))
print(id(b))
a = [1, 2, 3] # a为list类型
b = [1, 2, 3] # b为list类型
print(a is b)
print(a == b)
print(id(a))
print(id(b))
a = {'cheese': 1, 'zh': 2} # a为dict类型
b = {'cheese': 1, 'zh': 2} # b为dict类型
print(a is b)
print(a == b)
print(id(a))
print(id(b))
c = [1, 2, 3]
d = [1, 2, 3]
a = set(c) # a为set类型
b = set(d) # b为set类型
print(a is b)
print(a == b)
print(id(a))
print(id(b))
只有int、str、tuple类型的情况下,a is b才为True,id也相等,当a和b是list、dict或set型时,a is b为False,id不相等,is是对比地址,==是对比值
# 取出全部的key和value
dic = {
'name': '张波',
'age': 38,
'address': '国贸',
'phone': 13800138000,
'mail': '13800138000@qq.com',
'other': {
'interest': 'swimming',
'country': 'China',
'car': '北京奔驰'
}
}
keys = []
values = []
def get_kv(sh):
for k, v in sh.items():
if type(v) != dict:
keys.append(k)
values.append(v)
elif type(v) == dict:
for i, j in v.items():
keys.append(i)
values.append(j)
print('所有的key是:%s\n所有的value是:%s' % (keys, values))
get_kv(dic)