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

Python 如何过滤嵌套列表中的条件逻辑元素

Python 如何过滤嵌套列表中的条件逻辑元素,python,python-2.7,list,boolean-logic,Python,Python 2.7,List,Boolean Logic,我有一个给定任务的先决条件列表。该列表由构成逻辑表达式的其他任务(使用字母作为占位符)组成。示例如下所示: prereqList = [["A", "|", "B"], "&", [["C", "&", "D"], "|", "E"], "&", "F"] 我还有一个已完成任务的列表: completedTasks = ["A", "C", "F"] 我试图在遵循条件逻辑的同时从prereqList中删除已完成的任务。对于上述示例,我希望输出为: filtered_l

我有一个给定任务的先决条件列表。该列表由构成逻辑表达式的其他任务(使用字母作为占位符)组成。示例如下所示:

prereqList = [["A", "|", "B"], "&", [["C", "&", "D"], "|", "E"], "&", "F"]
我还有一个已完成任务的列表:

completedTasks = ["A", "C", "F"]
我试图在遵循条件逻辑的同时从prereqList中删除已完成的任务。对于上述示例,我希望输出为:

filtered_list = ["D", "|", "E"]
到目前为止,我所拥有的:

def filter_prereqs(prereqList, completedTasks):
    ops = {
            "&": (lambda a, b: a in completedTasks and b in completedTasks),
            "|": (lambda a, b: a in completedTasks or b in completedTasks)
        }
        for i in range(prereqList):
            if isinstance(prereqList[i], list):
                filter_prereqs(prereqList[i], completedTasks)
            else:
                if prereqList[i] not in ops.keys():
                    pass
                else:
                    conditionMet = ops[prereqList[i]](prereqList[i-1],prereqList[i+1])
                    if conditionMet:
我一直在想办法解决这个问题。任何帮助都将不胜感激

部分解决方案:

这是可行的,但只适用于“保守地括起来”的表达式。换言之,它可以解析
[“A”、“&”、“B”]、“&”、“C”]
,但不能解析
[“A”、“&”、“B”、“&”、“C”]
。所以我不能解析你问题中的确切表达式,但我可以解析一个逻辑上等价的版本,它有一对额外的括号

#sentinel value that gets returned for an expression that's 100% completed
complete = object()

def remove_completed(expression, completed):
    if isinstance(expression, str):
        if expression in completed:
            return complete
        else:
            return expression
    left, operator, right = expression
    left = remove_completed(left, completed)
    right = remove_completed(right, completed)
    if operator == "|":
        if left is complete or right is complete:
            return complete
        else:
            return [left, "|", right]
    elif operator == "&":
        if left is complete:
            return right
        elif right is complete:
            return left
        else:
            return [left, "&", right]

prereqList = [[["A", "|", "B"], "&", [["C", "&", "D"], "|", "E"]], "&", "F"]
result = remove_completed(prereqList, ["A", "C", "F"])
print(result)
结果:

['D', '|', 'E']

prereqList
[“A”,“and”,“B”]
,而
完成的任务是
[“A”,“B”]
对于范围内的i(prereqList):
没有意义支持
[“A”,“and”,“B”,“and”,“C”]
这样的表达式有多重要?我有一个想法,但它只适用于每个子列表正好有三个元素长的情况,例如
[[“A”、“&”、“B”]、“&”、“C”]
@Kevin在回应您的第一条评论时,预期结果将是一个空列表。对于您的第二条评论,大多数子列表都有3个元素长,但不是全部。@pault我使用范围内的prereqList来迭代列表,因此我可以在I之前和之后访问元素(I-1和I+1)。如果有更好的方法,我愿意接受任何建议。这很好。我应该能够在列表中使用保守的括号。谢谢你的帮助!