我正在尝试操作嵌套字典,以便将任何嵌套字典的键向后组合到只有一个键的情况下。你知道吗
我曾经尝试递归地这样做,但是我很难从字典中删除键并用串联键替换它们。你知道吗
例如:
{'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_'}
}
}
这是一个有趣的问题-可能有一个更优雅的解决方案,但我做到了以下几点:
输出:
相关问题 更多 >
编程相关推荐