基于logi从三个字符串列表生成输出列表

2024-04-20 02:07:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三个条件:过渡条件、真条件和假条件。你知道吗

  • 我想保持输入的顺序。你知道吗
  • 一个列表包含每个输入的转换,另一个列表包含每个输入的true条件,第三个列表包含每个输入的false条件。你知道吗
  • 我想循环遍历t_list中的项,并输出来自p_listn_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'],]

Tags: ortofalsetrue列表inputisitem
1条回答
网友
1楼 · 发布于 2024-04-20 02:07:37

这应该如预期的那样起作用:

def comb_gen(t_list, p_list, n_list, logic):
    if logic == 'OR':
        return [[p if j != i else s for j, p in enumerate(p_list)] for i, s in enumerate(t_list)]
    if logic == 'AND':
        return [[p if j != i else s for j, p in enumerate(n_list)] for i, s in enumerate(t_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']
>>> comb_gen(t_list, p_list, n_list, 'OR')
[['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']]
>>> comb_gen(t_list, p_list, n_list, 'AND')
[['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']]

相关问题 更多 >