我有三个条件:过渡条件、真条件和假条件。你知道吗
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'],]
这应该如预期的那样起作用:
输出:
相关问题 更多 >
编程相关推荐