首页 > 解决方案 > 一组 dicts 的 itertools.permutations

问题描述

在我问我的问题之前,我想让你们都知道我是一个初学者编码器,我感谢所有输入,所以提前感谢你的帮助。

假设我有以下对象:

set: [
       {'id': 1
        'type': a
       },

       {'id': 2
        'type': a
       },

       {'id': 3
        'type': b
       },

       {'id': 4
        'type': b
       },

       {'id': 5
        'type': a
       }
]

我怎么能用它itertools.permutations来计算(并最终复制)'type'密钥的所有组合?即有一个集合,其中 ids 3 和 5 具有类型b,其余的a,一个集合,其中 ids 2 和 5 具有b和其余的a,等等。

此外,是否可以在特定条件下计算组合(例如,id 1 和 2 必须始终是a所有组合的类型)?

谢谢大家

标签: pythondictionarykey-valueitertools

解决方案


基于John 已有的内容,我们可以通过在计算排列之前从类型列表中删除受约束的类型来限制排列。然后,您可以将类型重新插入到约束定义的每个排列中。以下示例脚本将返回带有约束的排列:“ids 1 和 2 必须始终为所有组合的类型 a”。随意删除打印语句。

from itertools import permutations

# these define which types must be located at the given id (index + 1)
constraints = [
    {'id': 1, 'type': "a"},
    {'id': 2, 'type': "a"}
]

# this is the list of types to permute
typeset = [
    {'id': 1, 'type': "a"},
    {'id': 2, 'type': "a"},
    {'id': 3, 'type': "b"},
    {'id': 4, 'type': "b"},
    {'id': 5, 'type': "a"}
]

# we create lists of types to permute and remove the ones that are constrained
types_to_permute = [d["type"] for d in typeset]
constraint_types = [d["type"] for d in constraints]
for constraint_type in constraint_types:
    types_to_permute.remove(constraint_type)
print(types_to_permute)

# we calculate the permutations for the unconstrained types
permutations = list(permutations(types_to_permute, r=None))
permutations = [list(permutation) for permutation in permutations]
print(permutations)

# we insert the constrained types in their given locations
for permutation in permutations:
    for constraint in constraints:
        permutation.insert(constraint["id"] - 1, constraint["type"])
print(permutations)

# we reconstruct the permutations in the orignal format (list of dictionaries)
final_permutations = []
for permutation in permutations:
    final_permutation = []
    for index, type_object in enumerate(permutation):
        final_permutation.append({"id": index + 1, "type": type_object})
    final_permutations.append(final_permutation)
print(final_permutations)

最终的排列将是:

[
    [
        {'id': 1, 'type': 'a'},
        {'id': 2, 'type': 'a'},
        {'id': 3, 'type': 'b'},
        {'id': 4, 'type': 'b'},
        {'id': 5, 'type': 'a'}],
    [
        {'id': 1, 'type': 'a'},
        {'id': 2, 'type': 'a'},
        {'id': 3, 'type': 'b'},
        {'id': 4, 'type': 'a'},
        {'id': 5, 'type': 'b'}],
    [
        {'id': 1, 'type': 'a'},
        {'id': 2, 'type': 'a'},
        {'id': 3, 'type': 'b'},
        {'id': 4, 'type': 'b'},
        {'id': 5, 'type': 'a'}],
    [
        {'id': 1, 'type': 'a'},
        {'id': 2, 'type': 'a'},
        {'id': 3, 'type': 'b'},
        {'id': 4, 'type': 'a'},
        {'id': 5, 'type': 'b'}],
    [
        {'id': 1, 'type': 'a'},
        {'id': 2, 'type': 'a'},
        {'id': 3, 'type': 'a'},
        {'id': 4, 'type': 'b'},
        {'id': 5, 'type': 'b'}],
    [
        {'id': 1, 'type': 'a'},
        {'id': 2, 'type': 'a'},
        {'id': 3, 'type': 'a'},
        {'id': 4, 'type': 'b'},
        {'id': 5, 'type': 'b'}]]

推荐阅读