Python 创建嵌套dict的置换

Python 创建嵌套dict的置换,python,Python,我正在编写一个代码,它应该检查注册表行,并为(1)每个协议使用哪些行,以及(2)为了检查某些功能而断开哪些行,创建所有排列 reg_lines = {'primary': ['ETH', 'UDP', 'TCP'], 'secondary': ['ETH', 'TCP'], private': ['UDP', 'TCP']} 预期排列: 一, 二, j 一, i+1 i+2 n 首先,使用itertools.combines获取主要、次要和专用的所有组合,然后使用itertools.pr

我正在编写一个代码,它应该检查注册表行,并为(1)每个协议使用哪些行,以及(2)为了检查某些功能而断开哪些行,创建所有排列

reg_lines = {'primary': ['ETH', 'UDP', 'TCP'], 'secondary': ['ETH', 'TCP'], private': ['UDP', 'TCP']}
预期排列:

一,

二,

j

一,

i+1

i+2

n


首先,使用
itertools.combines
获取主要、次要和专用的所有组合,然后使用
itertools.product
获取这些组合的乘积。然后再次使用
itertools.compositions
获取
disc
字典的所有子集

from itertools import product, combinations, chain

reg_lines = {'primary': ['ETH', 'UDP', 'TCP'], 'secondary': ['ETH', 'TCP'], 'private': ['UDP', 'TCP']}

def all_combinations(lst):
    return chain(*[combinations(lst, i+1) for i in range(len(lst))])

for comb in all_combinations(reg_lines):
    for prod in product(*(reg_lines[k] for k in comb)):
        use = dict(zip(comb, prod))
        print("use", use)
        for comb2 in all_combinations(comb):
            disc = {k: use[k] for k in comb2}
            print("disc", disc)
输出:

use {'primary': 'ETH'}
disc {'primary': 'ETH'}
use {'primary': 'UDP'}
disc {'primary': 'UDP'}
... many many more ...
use {'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP'}
disc {'primary': 'TCP'}
disc {'secondary': 'TCP'}
disc {'private': 'TCP'}
disc {'primary': 'TCP', 'secondary': 'TCP'}
disc {'primary': 'TCP', 'private': 'TCP'}
disc {'secondary': 'TCP', 'private': 'TCP'}
disc {'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP'}

尝试使用itertools.compositions,但我发现了一些非常混乱的问题。对我来说,问题不清楚
priv_line
在哪里,因为给定的
reg_line
@SIslam,您是对的。我简化了问题。删除了priv_行。在您的示例中,您不会得到仅“secondary”或“secondary”+“private”(use=dict(zip)(“primary”,“secondary”,“private”),x)的排列@alnet是的,问题IMHO中的这部分有点不清楚。现在呢?
use = [{'secondary: 'TCP'}]
disc = [{'secondary: 'TCP'}]
use = [{'primary': 'ETH', 'secondary': 'ETH'}
disc = [{'primary': 'ETH'}]
use = [{'primary': 'ETH', 'secondary': 'ETH'}]
disc = [{'primary': 'ETH'}]
use = [{'primary': 'ETH', 'secondary': 'ETH'}]
disc = [{'primary': 'ETH', 'secondary': 'ETH'}]
use = [{'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP}]
disc = [{'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP}]
from itertools import product, combinations, chain

reg_lines = {'primary': ['ETH', 'UDP', 'TCP'], 'secondary': ['ETH', 'TCP'], 'private': ['UDP', 'TCP']}

def all_combinations(lst):
    return chain(*[combinations(lst, i+1) for i in range(len(lst))])

for comb in all_combinations(reg_lines):
    for prod in product(*(reg_lines[k] for k in comb)):
        use = dict(zip(comb, prod))
        print("use", use)
        for comb2 in all_combinations(comb):
            disc = {k: use[k] for k in comb2}
            print("disc", disc)
use {'primary': 'ETH'}
disc {'primary': 'ETH'}
use {'primary': 'UDP'}
disc {'primary': 'UDP'}
... many many more ...
use {'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP'}
disc {'primary': 'TCP'}
disc {'secondary': 'TCP'}
disc {'private': 'TCP'}
disc {'primary': 'TCP', 'secondary': 'TCP'}
disc {'primary': 'TCP', 'private': 'TCP'}
disc {'secondary': 'TCP', 'private': 'TCP'}
disc {'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP'}