Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.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_Dictionary_Recursion - Fatal编程技术网

Python 使用递归删除

Python 使用递归删除,python,dictionary,recursion,Python,Dictionary,Recursion,我有下面的树状结构,由嵌套字典组成。每个字典都有一个元组作为键,并包含一个字典列表。列表可以为空 {(0, 26): [{(2, 9): [{(4, 7): []}, {(6, 9): []}, {(2, 5): []}, {(5, 8): []}, {(3, 6): []}, {(2, 7): [{(4, 7): []}, {

我有下面的树状结构,由嵌套字典组成。每个字典都有一个元组作为键,并包含一个字典列表。列表可以为空

 {(0, 26): [{(2, 9): [{(4, 7): []},
                {(6, 9): []},
                 {(2, 5): []},
                 {(5, 8): []},
                 {(3, 6): []},
                 {(2, 7): [{(4, 7): []}, {(2, 5): []}, {(3, 6): []}]},
                 {(3, 8): [{(4, 7): []}, {(5, 8): []}, {(3, 6): []}]}]},
            {(14, 27): [{(14, 17): []},
                  {(17, 20): []},
                  {(18, 21): []},
                  {(20, 23): []},
                  {(22, 25): []},
                   {(17, 21): [{(17, 20): []}, {(18, 21): []}]}]},
            {(8, 16): [{(12, 16): []},
                  {(8, 11): []},
                  {(10, 16): [{(12, 16): []}]},
                  {(9, 12): []}]},
            {(7, 14): [{(7, 12): [{(7, 10): []},
                             {(8, 11): []},
                             {(9, 12): []}]},
                  {(7, 10): []},
                  {(8, 11): []},
                  {(9, 12): []}]},
            {(1, 4): []}]}
我正在尝试删除可能包含在其他词典中的词典。如果“depth d”处的字典有一个包含在“depth d-1”处找到键的字典的列表,我希望例程删除“depth d-1”处的字典。 例如,键为(2,9)的字典包含键为(4,7)的字典。该词典实际上包含在同一词典中的另外两个词典中,其键为(2,7)和(3,8)。我想写一个例程,以获得以下结果。。。在这里,我只是展示了带有键(2,9)的dictionary的结果:


我在想,我应该能够使用递归来实现这一点,但我没有成功。这可以通过递归来解决吗?如果不是的话,有什么更好的解决方法呢?

那么,列表的内容是否重要?或者你只是在按键删除?另外,为了确认,如果一个条目是深度d,那么所有深度d-1及以下的条目都将被删除?你能告诉use你到底想做什么吗?您的例程需要做什么还不清楚。这些列表可以包含其他词典。如果一个列表包含一个字典,它的键在深度d-1处,那么我们希望删除深度d-1处的条目(但不是深度d处的条目)。是的,关于你的最后一个问题。@marcos:那么,如果我在深度
d
处有
(4,5):[]
,在深度
d-1
处有
(4,5):[…]#非空的
,这会被删除吗?好问题!只有相反的情况才能发生。也就是说,我在深度d-1(4,5):[]和深度d(4,5):[…]#非空
# Build a dict key:list of index
for i, d in enumerate(list_of_dict):
    key = d.keys()[0]
    if not key in dict_idx:
        dict_idx[key] = []
    dict_idx[key]. append(i)

# Delete from all multiple key the first 
for key in dict_idx:
    if len(dict_idx[key]) > 1:
        del list_of_dict[dict_idx[key][0]]
# Build a dict key:list of index
for i, d in enumerate(list_of_dict):
    key = d.keys()[0]
    if not key in dict_idx:
        dict_idx[key] = []
    dict_idx[key]. append(i)

# Delete from all multiple key the first 
for key in dict_idx:
    if len(dict_idx[key]) > 1:
        del list_of_dict[dict_idx[key][0]]