繁体   English   中英

如何获得不同对象的所有可能组合(列表或字符串)

[英]How to get all possible combinations of different objects (list or str)

这更像是一个一般性问题,我不确定是否存在任何算法来满足我的想法,以及它是否存在所谓的......

假设我们有三个对象(列表)

["object", 1 , "this is object one"]
["object2", 2 , "this is object one"]
["object3", 3 , "this is object three"]

我现在想做的是创建具有所有可能组合的新列表,例如

["object2", 1 , "this is object three"]
["object1", 3 , "this is object two"]

甚至未来

 [3, "object2", "this is object one"]
 [1 , "this is object 2" , ] # last item removed as part of going for all combinations

所以我正在寻找一种算法来混合/组合/添加/删除逻辑框架中的内容。

我知道 python 有但在 itertools lib 和组合方法中,但它似乎没有提供所有组合。

是否有任何已知的算法/库用于对象之间的路径和组合?

谢谢

你似乎不想要组合 您想要笛卡尔积,并且可能需要排列(从您的描述中看不清楚)。 对于笛卡尔积,您需要执行以下操作

itertools.product([
    ('object1', 'object2', 'object3'),
    (1, 2, 3),
    ('this is 1', 'this is 2', 'this is 3')
])

对于问题的第二部分,在上一个示例构建的列表的每个元素上,您可以使用itertools.permutations

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM