首页 > 解决方案 > 我的方法返回一个字典,我希望在方法的参数更改时更新这个字典

问题描述

我有一个名为的方法getsomething(url),它访问一个 url 并获取一些详细信息。此方法返回一个字典。现在随着 url 的变化,我想要这个已经创建的字典来更新新项目。如何才能做到这一点?

我试过这个:

url_list = ['http:something.com','anotherthing.com']

def getdetails(url):
    dict = {}
    # some lines of code here...
    return dict

它在这里被调用:

for url in url_list: 
    result = {}
    result = result.update(getdetails(url))

我收到类似的错误nonetype object is not iterable

我需要同一个字典中的两个网址的结果

标签: pythonpython-3.x

解决方案


要更新字典,您需要知道字典的外观。

#// empty dictionary
dic = {}

#// some itmes on dictionary
dic2 = {"one":1, "two":3}
print(dic2)

#// so to update one itme first u need to specifi what item (index) need to be updated
dic2.update(two=2)

print("new =", dic2)

终端:

{'one': 1, 'two': 3}
new = {'one': 1, 'two': 2}

还要检查这个。相信我,在谷歌上搜索并不丢人。我一直这样做是正常的。 https://www.journaldev.com/23232/python-add-to-dictionary

还要检查该“项目”是否 == None 以及是否为 !=None 他们使更新“其他”尝试检查错误并查看那里发生了什么。


推荐阅读