发布网友 发布时间:2022-04-18 03:00
共3个回答
懂视网 时间:2022-04-18 07:21
python中不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住,如下实例:
dict1 = {'Name': 'Runoob', 'Age': 7, 'Name': '小菜鸟'} print ("dict1['Name']: ", dict1['Name'])
如果是pycharm(community edition v3.4.4), 则pycharm会有提示: Dictionary contains duplicate keys 'Name' more...
实际打印最后一个值
python自带idle也会打印最后一个值
热心网友 时间:2022-04-18 04:29
按照你的要求编写的两个字典key相同的值组成新字典的Python程序如下
dic1={'福州龙湖': ['1293521.23', '620624'], '赣州龙湖': ['3050.51']}
dic2={'福州龙湖': ['12345', '67890'], '赣州龙湖': ['123','456']}
dic3={}
for key in dic1:
if dic2.get(key):
dic3[key]=[dic1[key],dic2[key]]
else:
dic3[key]=dic1[key]
for key in dic2:
if dic1.get(key):
pass
else:
dic3[key]=dic2[key]
print(dic3)
源代码(注意源代码的缩进)
运行结果
{'福州龙湖': [['1293521.23', '620624'], ['12345', '67890']], '赣州龙湖': [['3050.51'], ['123', '456']]}
追问您好还有个问题如果我是多个字典组成呢?
按照你的要求编写的多个字典key相同的值组成新字典的Python程序如下
def hebing(dic1,dic3):
for key in dic1:
if dic3.get(key):
dic3[key].append(dic1[key])
else:
dic3[key]=[dic1[key]]
return dic3
dic1={'福州龙湖': ['1293521.23', '620624'], '赣州龙湖': ['3050.51']}
dic2={'福州龙湖': ['12345', '67890'], '赣州龙湖': ['123','456']}
dic4={'福州龙湖': ['789', '888'], '北京龙湖': ['111']}
dic3={}
dic3=hebing(dic1,dic3)
dic3=hebing(dic2,dic3)
dic3=hebing(dic4,dic3)
print(dic3)
源代码(注意源代码的缩进)
运行结果
{'福州龙湖': [['1293521.23', '620624'], ['12345', '67890'], ['12345', '67890']], '赣州龙湖': [['3050.51'], ['123', '456']], '北京龙湖':[['111']]}
热心网友 时间:2022-04-18 05:47
需要你自己写判断,和操作