如何(递归或其他方式)解压缩包含索引为0的字符串和索引为1的列表的元组列表?

时间:2018-08-29 21:12:38

标签: python python-3.x

给出以下内容:

params = ['sn', 'tp', 'v1', 'temp', 'slew']

list_tuple = [('Serial Number', [12345]),
 ('Test Points', ['TestpointA', 'TestpointC']),
 ('Voltage_1', [3.0, 3.3, 3.6, 0.0]),
 ('Temperature Setpoint', [0, 60]),
 ('Slew_1', [200, 400, 800, 1600, 3200, 6400])]

def what_i_want(test_tuple, params):
    for sn in test_tuple[0][1]:
            for tp in test_tuple[1][1]:
                for v in test_tuple[2][1]:
                    for temp in test_tuple[3][1]:
                        for slew in test_tuple[4][1]:
                            print(f'{params[0]}: ', sn)
                            print(f'{params[1]}: ', tp)
                            print(f'{params[2]}: ', v)
                            print(f'{params[3]}: ', temp)
                            print(f'{params[4]}: ', slew)
                            print('\n')

what_i_want(list_tuple, params)

产生所需的输出

sn:  12345
tp:  TestpointA
v1:  3.0
temp:  0
slew:  200

sn:  12345
tp:  TestpointA
v1:  3.0
temp:  0
slew:  400
...
...

params的长度对应于list_tuple中的元组数,并且该长度可以变化。元组中每个列表的长度也可以变化(即序列号为3或4个元素的列表,而不是1个)。

我想使用递归来解包list_tuple并按索引正确调用params,所以我不必创建带有一堆嵌套循环的类。但是,这是我第一次使用递归,并且只能生成以下内容:

def poo_doo(list_tuple):
    for i in list_tuple:
        if isinstance(i, tuple):
            print(i[0])
            poo_doo(i[1])
        else:
            print(i)
            print('\n')

poo_doo(list_tuple)

产量:

Serial Number
12345


Test Points
TestpointA


TestpointC


Voltage_1
3.0


3.3
...

请协助您使用最有效的方法来产生所需的输出,最好不要使用嵌套循环
链接到完整的类,以提供更广泛的理解范围:auto_filter class

谢谢。

1 个答案:

答案 0 :(得分:3)

您可以使用itertools.product

import itertools
data = [('Serial Number', [12345]), ('Test Points', ['TestpointA', 'TestpointC']), ('Voltage_1', [3.0, 3.3, 3.6, 0.0]), ('Temperature Setpoint', [0, 60]), ('Slew_1', [200, 400, 800, 1600, 3200, 6400])]
params = ['sn', 'tp', 'v1', 'temp', 'slew']
for i in itertools.product(*[b for _, b in data]):
  print('\n'.join(f'{a}:{b}' for a, b in zip(params, i)))
  print('-'*20)

输出(前三个结果):

sn:12345
tp:TestpointA
v1:3.0
temp:0
slew:200
--------------------
sn:12345
tp:TestpointA
v1:3.0
temp:0
slew:400
--------------------
sn:12345
tp:TestpointA
v1:3.0
temp:0
slew:800
--------------------
...

虽然itertools.product是(也许)最干净的解决方案,但是可以使用带有生成器的简单递归函数:

def combination(d, current = []):
   if len(current) == len(data):
     yield current
   else:
     for i, a in enumerate(d):
       for c in a: 
         yield from combination(d[i+1:], current = current+[c])


for i in combination([b for _, b in data]):
  print('\n'.join(f'{a}:{b}' for a, b in zip(params, i)))
  print('-'*20)

输出(前三个结果):

sn:12345
tp:TestpointA
v1:3.0
temp:0
slew:200
--------------------
sn:12345
tp:TestpointA
v1:3.0
temp:0
slew:400
--------------------
sn:12345
tp:TestpointA
v1:3.0
temp:0
slew:800
--------------------