簡體   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