【问题标题】:Generate Output List from three string lists based on logic基于逻辑从三个字符串列表生成输出列表
【发布时间】:2019-11-20 17:52:05
【问题描述】:

我有三个条件:转换条件、真条件和假条件。

  • 我想保持输入的顺序。
  • 一个列表包含每个输入的转换,另一个列表包含每个输入的真实条件,第三个列表包含每个输入的错误条件。
  • 我想循环浏览 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'],]

【问题讨论】:

  • 为什么过渡在输出中是对角线的?
  • 因为我想始终保持输入的顺序相同。

标签: python python-3.x list list-comprehension itertools


【解决方案1】:

这应该可以按预期工作:

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']]

【讨论】:

  • 这是一个很棒的解决方案!非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 2018-11-24
相关资源
最近更新 更多