简体   繁体   English

我有一个包含 boolean 个值的列表列表。 如何输入 boolean function 并在列表的每个子列表中检查它?

[英]I have a list of lists containing boolean values. How can I input a boolean function and check it in every sublist of my list?

Here's what i am trying to do.这就是我想要做的。 I am basically having a truth-table for two boolean formulas:我基本上有两个 boolean 公式的真值表:

x=[True, False]
y=[True, False]
a=[]
for i in x:
    for z in y:
        a.append([i, z])

Now I want to input some boolean expression and check it in every "row" of my truth-table.现在我想输入一些 boolean 表达式并在我的真值表的每一“行”中检查它。 I tried this:我试过这个:

p=None
q=None
result=[]
exp=input("Type your boolean expression using p and q as variables: ")
for i in a:
    p, q = i[0], i[1]
    result.append(exp)
    print(result)

But when I try to type some boolean expression as input, for example:但是当我尝试输入一些 boolean 表达式作为输入时,例如:

 (not p) or q

It uses at as a string.它使用 at 作为字符串。 But if I do this:但如果我这样做:

exp=bool(input("Type your boolean expression using p and q as variables: "))

then every non-empty string would be regarded as True in bool .那么每个非空字符串在bool中都将被视为True How can I solve this?我该如何解决这个问题?

From what you said I understand that you want to apply a hand written expression to all elements of a list.根据您所说的,我了解到您想将手写表达式应用于列表的所有元素。

If your table is always 2-element wide you can go with:如果你的表总是 2 元素宽,你可以使用 go:

table = [[True, True], [False, True], [True, False]]  
expression  = 'p and q'  
[eval(expression) for p, q in table]  
# Output
[True, False, False]

However your expression need to respect Python syntax.但是,您的表达式需要遵守 Python 语法。 What's more eval is slow.更重要的是 eval 很慢。 So this answer can probably be enhanced.所以这个答案可能会得到加强。

More information about eval here: Documentation有关 eval 的更多信息,请参见:文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在包含多个子列表的列表中检查多个条件? - How can I check several conditions on a list containing several sublist? 如何检查嵌套列表的每个子列表中是否存在值? - How can I check if a value is present in every sublist of a nested list? 如何检查字符串是否在列表列表中,如果有则返回子列表? - How do I check if a string is in a list of lists, and return the sublist if it is? 如何将布尔函数应用于列表中的每个元素? - How can I apply a boolean function to each element in a list? 我有一本包含值列表的字典。 我想从值列表中取最高值 - I have a dictionary with lists of values. I want to take the highest value from the list of values 如何检查列表列表中的子列表? - How to check for a sublist in a list of lists? 如何将列表列表转换为键为整数且值为 integer 所属的子列表的索引的字典? - How can I convert a list of lists to a dictionary whose keys are integers and values are the index of the sublist to which the integer belongs? 如果包含列表的列包含来自另一个更大列表的元素,你如何 output boolean? - How do you output boolean if column containing lists have elements from another larger list? 战舰游戏:如何检查用户输入是否在列表列表中? - Battleship game: how can I check if user input is in a list of lists? 如何获取布尔值作为包含布尔值的python列表的列表的输出 - How to get boolean values as output on a list for a python list containing Boolean values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM