我的方法返回一个dictionary,我希望在方法的参数更改时更新这个dictionary

2024-09-28 22:36:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为getsomething(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的错误

我需要在同一个dict中的两个url的结果


Tags: 项目方法comhttpurl字典result细节
1条回答
网友
1楼 · 发布于 2024-09-28 22:36:06

要更新词典,你需要知道词典的外观。你知道吗

#// 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

还要检查“item”==None或note以及if!=None他们进行更新“else”尝试检查错误并查看那里发生了什么。你知道吗

相关问题 更多 >