Python:在深度嵌套的字典中更新值 [英] Python: Updating a value in a deeply nested dictionary

查看:831
本文介绍了Python:在深度嵌套的字典中更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在导入和处理一些深层嵌套的JSON(作为字典导入).它可以使用以下代码来分配值:

I am importing and manipulating some deeply nested JSON (imported as a dictionary). It can assign the values just fine using code like:

query['query']['function_score']['query']['multi_match']['operator'] = 'or'
query['query']['function_score']['query']['multi_match'].update({
        'minimum_should_match' : '80%' })  

但是,它既丑陋又笨拙.我想知道是否有一种更干净的方法来将值分配给合理有效的深度嵌套键?

But it's ugly and cumbersome as nuts. I'm wondering if there's a cleaner way to assign values to deep-nested keys that's reasonably efficient?

我已经读过关于可能使用内存中的SQLlite数据库的信息,但是经过一些操作之后,数据又回到了json中.

I've read about possibly using an in-memory SQLlite db, but the data is going back into json after a bit of manipulation.

推荐答案

multi_match = query['query']['function_score']['query']['multi_match']
multi_match['operator'] = 'or'
multi_match.update({'minimum_should_match' : '80%' })

这篇关于Python:在深度嵌套的字典中更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆