gpt4 book ai didi

python - 创建嵌套字典的排列

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:31 25 4
gpt4 key购买 nike

我正在编写一个代码,该代码应该遍历 reg_lines 并为(1)每个协议(protocol)使用哪些行以及(2)要断开哪些行以检查某些功能创建所有排列。

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

预期排列:

1.

use = [{'primary':'ETH'}]
disc = [{'primary':'ETH'}]
  • use = [{'primary': 'TCP'}]
    disc = [{'primary': 'TCP'}]

    ...

    j.

    use = [{'secondary': 'TCP'}]
    disc = [{'secondary': 'TCP'}]

    ...

    我。

    use = [{'primary': 'ETH', 'secondary': 'ETH'}
    disc = [{'primary': 'ETH'}]

    i+1。

    use = [{'primary': 'ETH', 'secondary': 'ETH'}]
    disc = [{'primary': 'ETH'}]

    i+2。

    use = [{'primary': 'ETH', 'secondary': 'ETH'}]
    disc = [{'primary': 'ETH', 'secondary': 'ETH'}]

    ...

    n.

    use = [{'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP'}]
    disc = [{'primary': 'TCP', 'secondary': 'TCP', 'private': 'TCP'}]

    最佳答案

    首先,使用 itertools.combinations 获取主要、次要和私有(private)的所有组合,然后使用 itertools.product 获取这些组合的乘积。然后再次使用 itertools.combinations 获取 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'}

    关于python - 创建嵌套字典的排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33410444/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com