Python:从嵌套的字典列表中删除项目,跳过元素

时间:2018-04-23 09:26:21

标签: python python-3.6

我无法从嵌套列表中删除项目,但似乎遇到了障碍。

我有许多测试目录,每个目录都有一个特定的测试模式。如果参数KEEP_DIR设置为True,我需要该目录。如果没有,我需要从列表中删除test_mode。

我尝试创建一个单独的测试模式列表,我不需要然后删除它们,而不对原始列表进行索引,但是一旦从dir_dict中删除了索引,我的代码就会跳过列表项。

我知道列表理解可能是最好的方法,但我看到的每个例子都是一维列表,我一直在努力去理解如何调整我在代码中看到的例子。

以下是我的嵌套列表:

{
    "TEST_DIRS": [
        {
            "BASE_DIR": "C:\\Path\\to\\files",
            "TEST_MODES": [
                {
                    "DIRS": {
                        "DIR_1": "C:\\Path\\to\\files\\tests\\",
                        "DIR_2": "C:\\Path\to\\files\\logs\\"
                    },
                    "FILES": {
                        "FILE_1": "C:\\Path\\to\\files\\tests\\file1",
                        "FILE_2": "C:\\Path\\to\\files\\tests\\file2"
                    },
                    "KEEP_DIR": true
                },
                {
                    "DIRS": {
                        "DIR_1": "C:\\another\\path\\to\\files\\tests\\",
                        "DIR_2": "C:\\another\\path\\to\\files\\log\\"
                    },
                    "FILES": {
                        "FILE_1": "C:\\another\\path\\to\\files\\tests\\file1",
                        "FILE_2": "C:\\another\\path\\to\\files\\tests\\file2"
                    },
                    "KEEP_DIR": false
                }
            ]
        }
    ]
}

以下是我正在使用的代码:

for i, base_dir in enumerate(dir_dict['TEST_DIRS']):
    for n, test_mode in enumerate(base_dir['TEST_MODES']):
        if not test_mode['KEEP_DIR']:
            fail = (i, n)
            del_list.append(fail)

for item in del_list:
    try:
        del dir_dict['TEST_DIRS'][item[0]]['TEST_MODES'][item[1]]
    except:
        print("Failed to delete:", item)

输出:

Deleting: (0, 0)
Deleting: (0, 1)
Deleting: (0, 2)
Deleting: (0, 3)
Deleting: (0, 4)
Deleting: (0, 5)
Deleting: (0, 6)
Deleting: (0, 7)
Deleting: (0, 8)
Deleting: (0, 9)
Failed to delete: (0, 9)
Deleting: (0, 10)
Failed to delete: (0, 10)
Deleting: (0, 12)
Failed to delete: (0, 12)
Deleting: (0, 13)
Failed to delete: (0, 13)
Deleting: (0, 14)
Failed to delete: (0, 14)
Deleting: (0, 15)
Failed to delete: (0, 15)
Deleting: (0, 16)
Failed to delete: (0, 16)

回溯: 当我删除try时,除了块 - 这是我得到的错误:

Traceback (most recent call last):
:
:
  File "check_files.py", line 180, in check_dir_dict
    del dir_dict['TEST_DIRS'][item[0]]['TEST_MODES'][item[1]]
IndexError: list assignment index out of range

非常感谢任何帮助。

解决方案: 如ZZ ll所述,反转del_list应该删除索引问题。 当我颠倒del_list的顺序时,索引仍然被删除但它们不会在每次删除之间切换。这看起来是目前最容易实施的解决方案:

for item in reversed(del_list):
    del dir_dict['SAGE_DIRS'][item[0]]['TEST_MODES'][item[1]]

感谢所有帮助:)

3 个答案:

答案 0 :(得分:2)

此处将以递归方式遍历您的数据结构,并删除包含'KEEP_DIR'个关键字'false'的所有词典:

def filter_out(orig):
  if isinstance(orig, list):
    return [ filter_out(element)
             for element in orig
             if (not isinstance(element, dict) or
                 element.get('KEEP_DIR', 'true') != 'false') ]
  elif isinstance(orig, dict):
    return { key: filter_out(value)
             for key, value in orig.items() }
  else:
    return orig

您可能希望使用False而不是'false'或类似内容进行调整,具体取决于您的实际值。 false在Python中无效。

dir_dict = {
    "TEST_DIRS": [
        {
            "BASE_DIR": "C:\\Path\\to\\files",
            "TEST_MODES": [
                {
                    "DIRS": {
                        "DIR_1": "C:\\Path\\to\\files\\tests\\",
                        "DIR_2": "C:\\Path\\to\\files\\logs\\",
                    },
                    "FILES": {
                        "FILE_1": "C:\\Path\\to\\files\\tests\\file1",
                        "FILE_2": "C:\\Path\\to\\files\\tests\\file2",
                    },
                    "KEEP_DIR": 'true',
                },
                {
                    "DIRS": {
                        "DIR_1": "C:\\another\\path\\to\\files\\tests\\",
                        "DIR_2": "C:\\another\\path\\to\\files\\log\\",
                    },
                    "FILES": {
                        "FILE_1": "C:\\another\\path\\to\\files\\tests\\file1",
                        "FILE_2": "C:\\another\\path\\to\\files\\tests\\file2",
                    },
                    "KEEP_DIR": 'false',
                },
           ]
        }
    ]
}

现在你可以这样称呼它:

import pprint
pprint.pprint(dir_dict)
{'TEST_DIRS': [{'BASE_DIR': 'C:\\Path\\to\\files',
                'TEST_MODES': [{'DIRS': {'DIR_1': 'C:\\Path\\to\\files\\tests\\',
                                         'DIR_2': 'C:\\Path\\to\\files\\logs\\'},
                                'FILES': {'FILE_1': 'C:\\Path\\to\\files\\tests\\file1',
                                          'FILE_2': 'C:\\Path\\to\\files\\tests\\file2'},
                                'KEEP_DIR': 'true'},
                               {'DIRS': {'DIR_1': 'C:\\another\\path\\to\\files\\tests\\',
                                         'DIR_2': 'C:\\another\\path\\to\\files\\log\\'},
                                'FILES': {'FILE_1': 'C:\\another\\path\\to\\files\\tests\\file1',
                                          'FILE_2': 'C:\\another\\path\\to\\files\\tests\\file2'},
                                'KEEP_DIR': 'false'}]}]}
pprint.pprint(filter_out(dir_dict))
{'TEST_DIRS': [{'BASE_DIR': 'C:\\Path\\to\\files',
                'TEST_MODES': [{'DIRS': {'DIR_1': 'C:\\Path\\to\\files\\tests\\',
                                         'DIR_2': 'C:\\Path\\to\\files\\logs\\'},
                                'FILES': {'FILE_1': 'C:\\Path\\to\\files\\tests\\file1',
                                          'FILE_2': 'C:\\Path\\to\\files\\tests\\file2'},
                                'KEEP_DIR': 'true'}]}]}

答案 1 :(得分:0)

问题是dir_dict [' TEST_DIRS'] [item [0]] [' TEST_MODES']的长度发生了变化!

a = [1,2,3]
del a[0] => a = [2, 3]
del a[1] => a = [2] but your code assumes a = [3]
del a[2] => crash because a is now too small

所以你必须考虑到在特定元素之前删除元素时特定元素的索引减少的事实,或者你应该以相反的顺序从列表中删除元素:

a = [1,2,3]
del a[1] => a = [1, 3]
del a[0] => a = [3]

答案 2 :(得分:0)

import json
from pprint import pprint

with open('test.json', 'r') as f:
    dir_dict = json.loads(f.read())
# to load from your file or data structure
# do dir_dict = json.load(<varname>) if it's a object if a string use json.loads

del_list = []

for i, base_dir in enumerate(dir_dict['TEST_DIRS']):
    for n, test_mode in enumerate(base_dir['TEST_MODES']):
        if not test_mode['KEEP_DIR']:
            fail = (i, n)
            del_list.append(fail)

for item in del_list:
    try:
        del dir_dict['TEST_DIRS'][item[0]]['TEST_MODES'][item[1]]
    except:
        print("Failed to delete:", item)

pprint(dir_dict)

更正JSON数据,test.json:

{
    "TEST_DIRS": [
        {
            "BASE_DIR": "C:\\Path\\to\\files",
            "TEST_MODES": [
                {
                    "DIRS": {
                        "DIR_1": "C:\\Path\\to\\files\\tests\\",
                        "DIR_2": "C:\\Path\to\\files\\logs\\"
                    },
                    "FILES": {
                        "FILE_1": "C:\\Path\\to\\files\\tests\\file1",
                        "FILE_2": "C:\\Path\\to\\files\\tests\\file2"
                    },
                    "KEEP_DIR": true
                },
                {
                    "DIRS": {
                        "DIR_1": "C:\\another\\path\\to\\files\\tests\\",
                        "DIR_2": "C:\\another\\path\\to\\files\\log\\"
                    },
                    "FILES": {
                        "FILE_1": "C:\\another\\path\\to\\files\\tests\\file1",
                        "FILE_2": "C:\\another\\path\\to\\files\\tests\\file2"
                    },
                    "KEEP_DIR": false
                }
            ]
        }
    ]
}

当我使用正确的JSON数据时,一切都很适合我,没有抛出任何错误和输出是预期的。输出:已删除 [(0, 1)] (del_list)

最后输出(如预期的那样):

{'TEST_DIRS': [{'BASE_DIR': 'C:\\Path\\to\\files',
                'TEST_MODES': [{'DIRS': {'DIR_1': 'C:\\Path\\to\\files\\tests\\',
                                         'DIR_2': 'C:\\Path\to\\files\\logs\\'},
                                'FILES': {'FILE_1': 'C:\\Path\\to\\files\\tests\\file1',
                                          'FILE_2': 'C:\\Path\\to\\files\\tests\\file2'},
                                'KEEP_DIR': True}]}]}