微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一组字典的 itertools.permutations

如何解决一组字典的 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)?

先谢谢大家

解决方法

what John already has 为基础,我们可以通过在计算排列之前从类型列表中删除受约束的类型来约束您所描述的排列。然后,您可以将类型重新插入约束定义的每个排列中。以下示例脚本将返回带有约束的排列:“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,{'id': 3,'type': "b"},{'id': 4,{'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'},'type': 'b'},'type': 'a'}],[
        {'id': 1,'type': 'b'}],'type': 'b'}]]
,

您可以使用列表推导式提取所有 "type" 值,然后您可以将它们推送到 set(或将其留在 list 中)来计算排列。

from itertools import permutations

typeset = [...]  # what you named `set` in the question
types = [d["type"] for d in typeset]

# Optionally,push `types` through a `set` to get only the unique `type` values
# types = set(types)

combos = list(permutations(types,r=None))

如果您还没有看过,here's a link to the docs on permutationsr 是完全可选的,但它可以让您计算长度为 r 而不是 len(types) 的排列。


或者,让我们考虑一下您想要排列 List[Dict] 的情况:

from itertools import permutations

typeset = [...]  # as before
types = range(len(typeset))

combos = list(permutations(types,r=None))

您现在可以根据存储在 typeset 中的索引检索 combos 的所有排列。但是,根据您的 typeset 的大小,我不建议这样做,因为这会消耗相当多的内存。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?