python - 根据条件更新python dict值?
问题描述
我的字典如下
user_model = {
"users": [{
"userid": 100,
"level": 1,
"score": 5,
},
{
"userid": 101,
"level": 2,
"score": 5,
},
{
"userid": 100,
"level": 2,
"score": 5,
},
{
"userid": 103,
"level": 1,
"score": 2,
}
]
}
如果我的“用户 ID”为 103 且“级别”为 2,有人可以指点我正确的方向以仅更新“分数”。
谢谢
解决方案
这样您就可以列出 json 中的所有数据:
for item in user_model['users']:
print(item) #check items
if item['userid'] == 103:
item['score'] = 25
print(item)
推荐阅读
- reactjs - React 中的生命周期钩子是什么?什么时候使用它们?
- node.js - 尚未设置查找方法。PersistedModel 未正确附加到 DataSource
- antlr - antlr4:为什么`tree`不能用作规则名称?
- python - 如何仅在 anaconda 上列出过时的 pip 包?
- javascript - 库方法如何在 javascript 中工作?
- git - 使用 git merge 防止不必要的提交
- android-viewpager - NestedScrollView + Appbar 将视图推到顶部
- python - 如何修复有助于根据食谱准备菜肴的功能?
- kotlin - AWS Lambda + API 网关“无访问控制允许来源”
- angular - Angular 6 - 在建立连接之前关闭 WebSocket