Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据值的数量连接嵌套字典键_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 根据值的数量连接嵌套字典键

Python 根据值的数量连接嵌套字典键,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我正在尝试操作嵌套字典,以便只使用一个键向后组合任何嵌套字典的键 我曾尝试递归地执行此操作,但我很难从字典中删除键并用串联键替换它们 例如: {'adv': {'e': {'r': { 's': {'e': {'_end_': '_end_'} }, 't

我正在尝试操作嵌套字典,以便只使用一个键向后组合任何嵌套字典的键

我曾尝试递归地执行此操作,但我很难从字典中删除键并用串联键替换它们

例如:

{'adv':
    {'e':
        {'r':
            {
                's':
                    {'e':
                         {'_end_': '_end_'}
                     },
                't':
                    {'_end_': '_end_',
                     'i':
                         {'s':
                              {'e':
                                   {'r':
                                        {'_end_': '_end_'}
                                    }
                               }
                          }
                     }
            }
        },
        'i': {'c': {'e': {'_end_': '_end_'}
                    }
              }
    }
}
将成为

{'adv':
    {'er':
        {
            'se':
                {'_end_': '_end_'},
        't':
            {'_end_': '_end_',
             'iser':
                 {'_end_': '_end_'}

             }
        },
        'ice':
                 {'_end_': '_end_'}
    }
}

这是一个有趣的问题-可能有一个更优雅的解决方案,但我做了以下工作:

import pprint

t={'adv': {'e': {'r': {'s': {'e': {'_end_': '_end_'}},
                       't': {'_end_': '_end_',
                             'i': {'s': {'e': {'r': {'_end_': '_end_'}}}}}}},
           'i': {'c': {'e': {'_end_': '_end_'}}}}}


def concat_dict(d):
    if d == '_end_':
        return '_end_'

    rv = {}

    for k, v in d.items():
        if '_end_' in v:
            rv[k] = concat_dict(v)
        elif len(list(x for x in v.keys() if x != '_end_')) == 1:
            top_str = k
            next_str = list(v.keys())[0]
            rv[top_str + next_str] = concat_dict(v[next_str])
        else:
            rv[k] = concat_dict(v)
    return rv

def format_dict(d):
    while concat_dict(d) != d:
        d = concat_dict(d)
    return d


pprint.pprint(format_dict(t))
输出:

{'adv': {'er': {'se': {'_end_': '_end_'},
                't': {'_end_': '_end_', 'iser': {'_end_': '_end_'}}},
         'ice': {'_end_': '_end_'}}}

这是一个有趣的问题-可能有一个更优雅的解决方案,但我做了以下工作:

import pprint

t={'adv': {'e': {'r': {'s': {'e': {'_end_': '_end_'}},
                       't': {'_end_': '_end_',
                             'i': {'s': {'e': {'r': {'_end_': '_end_'}}}}}}},
           'i': {'c': {'e': {'_end_': '_end_'}}}}}


def concat_dict(d):
    if d == '_end_':
        return '_end_'

    rv = {}

    for k, v in d.items():
        if '_end_' in v:
            rv[k] = concat_dict(v)
        elif len(list(x for x in v.keys() if x != '_end_')) == 1:
            top_str = k
            next_str = list(v.keys())[0]
            rv[top_str + next_str] = concat_dict(v[next_str])
        else:
            rv[k] = concat_dict(v)
    return rv

def format_dict(d):
    while concat_dict(d) != d:
        d = concat_dict(d)
    return d


pprint.pprint(format_dict(t))
输出:

{'adv': {'er': {'se': {'_end_': '_end_'},
                't': {'_end_': '_end_', 'iser': {'_end_': '_end_'}}},
         'ice': {'_end_': '_end_'}}}

我不完全理解你对这个问题的解释,但我觉得你应该从调查开始。键是否得到特殊处理?似乎最终的对象都有一个键,如果你向后工作,应该与它们的父对象结合起来?结束键只是表示单词的结束。应将其视为与任何其他键/值相同。请解释所需输出背后的基本原理。算法,一步一步如果要以相同的方式处理,为什么不在所需的输出中将其连接到其父级。它通过了单键词典的测试。我不完全理解你对这个问题的解释,但我觉得你应该从调查开始。这个
\u end\u
键是否得到了特殊处理?似乎最终的对象都有一个键,如果你向后工作,应该与它们的父对象结合起来?结束键只是表示单词的结束。应将其视为与任何其他键/值相同。请解释所需输出背后的基本原理。算法,一步一步如果要以相同的方式处理,为什么不在所需的输出中将其连接到其父级。它通过了单键词典的测试。