gpt4 book ai didi

Python 字典没有更新

转载 作者:行者123 更新时间:2023-11-28 19:59:32 24 4
gpt4 key购买 nike

def mergeDict(object):
dict1 = {}
for i in range(len(object)):
dict1.update({'id': object[i].id, 'name': object[i].name, 'age': object[i].age, 'location': object[i].location})
return dict1
merged_dict = mergeDict(details_sorted)

但这行不通。

我想得到这样的东西:

{1: {'id': 1, 'name': 'John', 'age': '25'; 'location': 'somewhere'},
2: {'id': 2, 'name': ......}}

最佳答案

看起来 return 语句在 for 循环中,这意味着它只会返回第一个字典的更新。

关于Python 字典没有更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4109712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com