生成真值表但在python中保留一些未定义的变量

时间:2015-04-16 02:21:48

标签: python

输入:

vv = [True, False, None, True, None]

有没有办法通过一次替换未定义的一个布尔值来生成真值表 所以ouptut将是

[True, False, True, True, None]
[True, False, False, True, None]
[True, False, None, True, False]
[True, False, None, True, True]

1 个答案:

答案 0 :(得分:2)

假设Undefined可以是None,例如,您可以执行以下操作:

l = [True, False, None, True, None]

# first get indices of None/Undefined elements in the input list
indexes = [i for i,v in enumerate(l) if v is None]

# for each index, generate new list using True and False
# as substitutes.
for idx in indexes:
    for sub in [True, False]:
        new_l = l[:]
        new_l[idx] = sub
        print(new_l)

这给出了:

[True, False, True, True, None]
[True, False, False, True, None]
[True, False, None, True, True]
[True, False, None, True, False]