【python】字典添加新值,不替换原来的旧值

names = ['John', 'Keven']
ages = [10, 12]

mydict = dict()
for name, age in zip(names, ages):
   # pay attention here!!!
    each = dict()
    print(each) # {}
    each['name'] = name
    print(each) # {'name': 'John'}
    each['age'] = age
    print(each) # {'name': 'John', 'age': 10}
    mydict[name] = each

print(mydict) # {'John': {'name': 'John', 'age': 10}, 'Keven': {'name': 'Keven', 'age': 12}}

版权声明:本文为m0_60255218原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>