python - 如何在for循环中将数据添加到字典
问题描述
我想知道如何在 for 循环中更新具有某些结果的字典。
目前,我有这样的事情:
some_dict = {}
result = call_to_some_method(some_data)
some_dict.update(json.loads(result))
我现在将代码更改如下:
some_dict = {}
for i in ["cat","cat2","zeta"]:
result = call_to_some_method(some_data)
## Update the result for each iteration
有人可以帮忙吗,这样最后我会像以前一样返回字典
解决方案
我认为您想要做的是以下内容:
some_dict = {}
for i in ["cat","cat2","zeta"]:
result = call_to_some_method(i)
some_dict[i] = result # This updates the dictionary
推荐阅读
- vb.net - 表达式不是一种方法——冒泡排序
- python - 在 Python 中使用 XGboost_Regressor 可以获得非常好的训练性能,但预测效果很差
- matlab - matlab 代码如何给出奇怪的输出以及如何解释它?
- git - 在前端分支上启动服务器时使用后端分支
- angular - 可观察多个 MatTableDataSource
- rfid - 什么决定了 Mifare 设备的 SAK?
- parsing - 确定语法类型
- c++ - 语言符号和向量值
- angular - Angular 7 中的自动完成材质 Angular
- python - Scrapy:通过管道发送到数据库时包含具有 404 状态代码的项目