【发布时间】:2019-11-20 17:52:05
【问题描述】:
我有三个条件:转换条件、真条件和假条件。
- 我想保持输入的顺序。
- 一个列表包含每个输入的转换,另一个列表包含每个输入的真实条件,第三个列表包含每个输入的错误条件。
- 我想循环浏览
t_list中的项目并输出来自p_list或n_list的其他输入,但不是正在转换的输入。 - 如果逻辑是
'OR'我想使用n_list,否则-如果逻辑是'AND'我想使用p_list。
t_list = ['Input 1 transitions to true', 'Input 2 transitions to true', 'Input 3 transitions to true']
p_list = ['Input 1 is true', 'Input 2 is true', 'Input 3 is true']
n_list = ['Input 1 is false', 'Input 2 is false', 'Input 3 is false']
我正在定义一种方法来为我的输出条件生成第四个列表。
num_inputs = len(t_List)
logic = 'OR' #or 'AND' based on prior input
def combination_generator (t_List, p_List, n_List, logic, num_inputs):
count = 0
final_array = []
temp_array = []
for item in t_List:
temp_array.append(item)
if logic == 'OR':
for item in n_List:
temp_array.append(item)
elif logic == 'AND':
for item in p_List:
temp_array.append(item)
我最初的解决方案是使用itertools.combinations(),如下所示:
for x in itertools.combinations(temp_array, num_inputs):
#file.write(f'{count} {x}\n')
count+=1
final_array.append(x)
我根据计数值手动选择了要附加到输出数组的输出组合。
我觉得好像有更好的解决方案,也许是列表推导。
final_list = [item for item in n_List if logic == 'OR']
理想输出:
'AND':
output_array = [['Input 1 transitions to true', 'Input 2 is true', 'Input 3 is true'],
['Input 1 is true', 'Input 2 transitions to true', 'Input 3 is true'],
['Input 1 is true', 'Input 2 is true', 'Input 3 transitions to true'],]
'OR':
output_array = [['Input 1 transitions to true', 'Input 2 is false', 'Input 3 is false'],
['Input 1 is false', 'Input 2 transitions to true', 'Input 3 is false'],
['Input 1 is false', 'Input 2 is false', 'Input 3 transitions to true'],]
【问题讨论】:
-
为什么过渡在输出中是对角线的?
-
因为我想始终保持输入的顺序相同。
标签: python python-3.x list list-comprehension itertools