python - 我的方法返回一个字典,我希望在方法的参数更改时更新这个字典
问题描述
我有一个名为的方法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
我需要同一个字典中的两个网址的结果
解决方案
要更新字典,您需要知道字典的外观。
#// 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 他们使更新“其他”尝试检查错误并查看那里发生了什么。
推荐阅读
- windows - 比 LogonUser() 更好的身份验证方式
- c - 二维数组的指针算术类型转换(C)
- mysql - MySQL 使用 C API 准备语句中的语法错误
- python - 在 python 中可视化具有(半)不透明的六边形网格的管
- python - python选择列表,然后从获胜列表中选择一个项目
- javascript - 异步函数 - 数据函数应该返回一个对象?
- php - PHP DomDocument - 自结束标记和特殊字符
- angular - 以角度对树节点的子节点进行排序
- php - SQLSTATE[HY000] [2002] 没有这样的文件或目录 (SQL: select * from `users` where `email` = test@test.com limit 1)
- angular - 从 Cloud Firestore 中检索数据