python:dict中列表的组合,具有各种大小的列表

时间:2014-10-27 14:47:48

标签: python list dictionary combinations

五个参数可以有各种len(包括空)。我为前三个参数编写了长代码,对于更多的参数来说,它是重复代码更大的代码。如何最小化代码?

params1 = [1,2]
params2 = []
params3 = [3,4,5,6]
# ...
params5 = [7,8]

# for 3 params example:
summary = {}
if 0 < len(params1):
    for param1 in params1:
        summary['param1'] = param1
        if 0 < len(params2):
            for param2 in params2:
                summary['param2'] = param2
                if 0 < len(params3):
                    for param3 in params3:
                        summary['param3'] = param3
                        print summary
                else:
                    print summary
        else:
            if 0 < len(params3):
                for param3 in params3:
                    summary['param3'] = param3
                    print summary
            else:
                print summary
else:
    if 0 < len(params2):
        for param2 in params2:
            summary['param2'] = param2
            if 0 < len(param3):
                for param3 in params3:
                    summary['param3'] = param3
                    print summary
            else:
                print summary
    else:
        if 0 < len(params3):
            for param3 in params3:
                summary['param3'] = param3
                print summary
        else:
            print summary

需要使用非空参数组合获取dict,输出示例代码:

{'param3': 3, 'param1': 1}
{'param3': 4, 'param1': 1}
{'param3': 5, 'param1': 1}
{'param3': 6, 'param1': 1}
{'param3': 3, 'param1': 2}
{'param3': 4, 'param1': 2}
{'param3': 5, 'param1': 2}
{'param3': 6, 'param1': 2}

如何将示例代码替换为最小的行?

2 个答案:

答案 0 :(得分:0)

>>> import itertools    
>>> params1 = [1,2]
>>> params2 = [None]
>>> params3 = [3,4,5,6]
>>> # ...
>>> params5 = [7,8]
>>> print [x for x in itertools.product(params1, params2, ... , params5)]
[(1, None, 3), (1, None, 4), (1, None, 5), (1, None, 6), (2, None, 3), (2, None, 4), (2, None, 5), (2, None, 6)]

答案 1 :(得分:0)

你可以使用递归来实现:

def affich(params, summary = None):
#print(params, summary )
if len(params) == 0:
    print summary
else:
    if summary is None: summary = {}
    name, pars = params[0]
    if len(pars) == 0:
        affich(params[1:], summary )
    else:
        for p in pars:
            cur[name] = p
            affich(params[1:], summary )

affich((('params1', [1, 2]), ('params2', []), ('params3', [3, 4, 5, 6]), ('params5', [7, 8]))

给出:

{'params5': 7, 'params1': 1, 'params3': 3}
{'params5': 8, 'params1': 1, 'params3': 3}
{'params5': 7, 'params1': 1, 'params3': 4}
{'params5': 8, 'params1': 1, 'params3': 4}
{'params5': 7, 'params1': 1, 'params3': 5}
{'params5': 8, 'params1': 1, 'params3': 5}
{'params5': 7, 'params1': 1, 'params3': 6}
{'params5': 8, 'params1': 1, 'params3': 6}
{'params5': 7, 'params1': 2, 'params3': 3}
{'params5': 8, 'params1': 2, 'params3': 3}
{'params5': 7, 'params1': 2, 'params3': 4}
{'params5': 8, 'params1': 2, 'params3': 4}
{'params5': 7, 'params1': 2, 'params3': 5}
{'params5': 8, 'params1': 2, 'params3': 5}
{'params5': 7, 'params1': 2, 'params3': 6}
{'params5': 8, 'params1': 2, 'params3': 6}