我有这个:
[{'name': 'Peter'}, {'name': 'Anna'}]
我想把它弄出来:
[{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]
所以我做了:
>>> li = [{'name': 'Peter'}, {'name': 'Anna'}]
>>> new_li = []
>>> dic = {}
>>> for i in li:
... dic["name"] = i["name"] + " Williams"
... new_li.append(dic)
但:
>>> new_li
[{'name': 'Anna Williams'}, {'name': 'Anna Williams'}]
为什么?
你也能展示如何最好地得到[{'name': 'Peter Williams'}, {'name': 'Anna Williams'}]
吗?
编辑
我不理解这种行为的原因是因为我假设:
>>> dict = {'name':'Peter'}
>>> lis = [dict]
>>> dict['name'] = 'Olaf'
在哪里
>>> print lis
给
[{'name': 'Peter'}]
虽然它实际上是
[{'name': 'Olaf'}]