您能否建议是否有任何方法可以通过添加前缀或后缀来保留所有可重复(重复)的键。在下面的示例中,地址键重复 3 次。它可能会有所不同(1 到 3 次)。我想获得预期输出中的输出,并添加后缀以使密钥唯一。目前更新功能正在覆盖键值。
list = ['name:John','age:25','Address:Chicago','Address:Phoenix','Address:Washington','email:[email protected]']
dic = {}
for i in list:
j=i.split(':')
dic.update({j[0]:j[1]})
print(dic)
当前输出:{'name':'John','age':'25','Address':'Washington','email':'[email protected]'}
预期输出:{'name':'John','age':'25','Address1':'芝加哥','Address2':'Phoenix','Address3':'华盛顿','email':'John @email.com'}
尝试了以下方法:
list = ['name:John','age:25','Address:Chicago','Address:Phoenix','Address:Washington','email:[email protected]']
dic = {}
for i in list:
j=i.split(':')
dic.update({j[0]:j[1]})
print(dic)
预期输出:{'name':'John','age':'25','Address1':'芝加哥','Address2':'Phoenix','Address3':'华盛顿','email':'John @email.com'}