如何获得不同对象的所有可能组合(列表或字符串)
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
所以我正在寻找一种算法来 mix/combine/add/remove 逻辑框架中的东西。
我知道 python 有,但在 itertools 库和组合方法中,但它似乎没有提供所有组合。
是否有任何已知的 algorithm/lib 路径和对象之间的组合?
谢谢
您似乎不需要组合。您需要 Cartesian product,并且可能需要排列(根据您的描述有点不清楚)。对于笛卡尔积,你需要做类似
的事情
itertools.product([
('object1', 'object2', 'object3'),
(1, 2, 3),
('this is 1', 'this is 2', 'this is 3')
])
对于问题的第二部分,在前面示例构建的列表的每个元素上,您可以使用 itertools.permutations
。
这更像是一个一般性问题,我不确定是否存在我所想的任何算法,以及它是否存在所谓的...
假设我们有三个对象(列表)
["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
所以我正在寻找一种算法来 mix/combine/add/remove 逻辑框架中的东西。
我知道 python 有,但在 itertools 库和组合方法中,但它似乎没有提供所有组合。
是否有任何已知的 algorithm/lib 路径和对象之间的组合?
谢谢
您似乎不需要组合。您需要 Cartesian product,并且可能需要排列(根据您的描述有点不清楚)。对于笛卡尔积,你需要做类似
的事情itertools.product([
('object1', 'object2', 'object3'),
(1, 2, 3),
('this is 1', 'this is 2', 'this is 3')
])
对于问题的第二部分,在前面示例构建的列表的每个元素上,您可以使用 itertools.permutations
。