简体   繁体   中英

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

it's more like a general question and I'm not sure if any algorithm exists for what I have in mind and if it exists what its called ...

let's say we have three objects (list)

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

what I want to do now is create new lists with all possible combinations such as

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

and even future

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

so I'm looking for an algorithm to mix/combine/add/remove stuff in a logical framework.

I know python has but in itertools lib and combinations method but it doesn't seem to offer all combinations.

is there any known algorithm/lib for path and combinations between objects?

Thanks

You don't seem to want combinations . You want Cartesian product , and, probably, permutations (it's a little unclear from your description). For the Cartesian product, you need to do something like

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

For the second part of your question, on every element of the list built by the previous example, you can use itertools.permutations .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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