简体   繁体   English

Python递归修改字典

[英]Python modify dictionary recursively

I have a Python AST parser that converts expressions like 1==1 or 2==2 or 3==3 into this dictionary: 我有一个Python AST解析器,可将1==1 or 2==2 or 3==3类的表达式转换成该字典:

{ 'args': [ {'args': [{'value': 1}, {'value': 1}], 'function': '=='},
            {'args': [{'value': 2}, {'value': 2}], 'function': '=='},
            {'args': [{'value': 3}, {'value': 3}], 'function': '=='}],
  'function': 'or'}

This is then converted to JSON and POSTed to and API endpoint, however the API only understands binary or operations. 然后将其转换为JSON并发布到API端点,但是API仅理解二进制or操作。 So the pasted dictionary above needs to be converted (nested) into the following: 因此,上面粘贴的字典需要转换(嵌套)为以下内容:

{ 'args': [ { 'args': [ { 'args': [{'value': 1}, {'value': 1}],
                          'function': '=='},
                        { 'args': [{'value': 2}, {'value': 2}],
                          'function': '=='}],
              'function': 'or'},
            {'args': [{'value': 3}, {'value': 3}], 'function': '=='}],
  'function': 'or'}

Doing this from the Python AST is not possible because the BoolOp has 3 elements so I think it's best to do it recursively. 无法通过Python AST进行此操作,因为BoolOp具有3个元素,因此我认为最好以递归方式进行。 However I can't visualize a correct way to achieve this iterating the dictionary in a recursive manner. 但是,我无法以可视化的方式来实现以递归方式迭代字典的方法。

edit: 编辑:

The dictionary can be deeply nested like: 字典可以像下面这样深层嵌套:

{ 'args': [ {'args': [{'value': 1}, {'value': 1}], 'function': '=='},
            {'args': [{'value': 2}, {'value': 2}], 'function': '=='},
            { 'args': [ { 'args': [{'value': 3}, {'value': 3}],
                          'function': '=='},
                        { 'args': [{'value': 4}, {'value': 4}],
                          'function': '=='},
                        { 'args': [{'value': 5}, {'value': 5}],
                          'function': '=='}],
              'function': 'or'}],
  'function': 'and'}

You could do something like this: 您可以执行以下操作:

def _nest_ors(args):
    assert len(args) >= 2

    if len(args) == 2:
        return {
            'function': 'or',
            'args': args
        }

    return {
        'function': 'or',
        'args': [args[0], _nest_ors(args[1:])]
    }


def fix_ors(ast):
    assert ast['function'] == 'or'

    return _nest_ors(ast['args'])


expression = {'args': [{'args': [{'value': 1}, {'value': 1}], 'function': '=='},
                       {'args': [{'value': 2}, {'value': 2}], 'function': '=='},
                       {'args': [{'value': 3}, {'value': 3}], 'function': '=='}],
              'function': 'or'}

print(fix_ors(expression))

If you have really huge expressions, you will have to replace the recursion with an iterative solution. 如果您有非常大的表达式,则必须用迭代解决方案替换递归。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM