Py学习  »  Python

Python3字典列表

huang cheng • 4 年前 • 1492 次点击  

在处理python3中的词典列表时,我遇到了一个奇怪的现象

oldlist = [{'name':'cheng'}]
ages = '18,20'
newlist = []

for ele in oldlist:
    for age in ages.split(','):
       ele['age'] = age
       newlist.append(ele)

print(newlist)

The result is :
[{'name': 'cheng', 'age': '20'}, {'name': 'cheng', 'age': '20'}]

What I am expected is 
[{'name': 'cheng', 'age': '18'}, {'name': 'cheng', 'age': '20'}]
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129995
文章 [ 1 ]  |  最新文章 4 年前