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

标签 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, except block 时 - 这是我收到的错误:

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]]

感谢您的帮助:)

最佳答案

这里将递归地遍历您的数据结构,并删除包含 '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'}]}]}

关于Python:从嵌套字典列表中删除项目,跳过元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49977561/

相关文章:

python - 求和一列值,包括 Python 中的字母

python - 通过共享对象的 __dict__ 属性创建对象 View

python-3.6 - 使用 kinit.stdin.write 'str' % 密码时出现 TypeError : a bytes-like object is required, 而不是 ('%s\n')

matplotlib - 不能将单个艺术家放在一个以上的数字中

unit-testing - 如何模拟由另一个类方法调用的类方法?

python - 与内联相比,for 循环中元组拆包的行为

python - 维基百科信息框的内容

python - 如何用 pandas 读取大型 csv?

python - 如何使用 python3 安装 selenium?

python-3.x - 为什么 cv2.addweighted() 给出一个错误,即操作既不是 'array op array' ,也不是 'array op scalar' ,也不是 ' scalar op array' ?