How to form all possible combinations from n lists


K split X

This question is hard to put in a title, so an example is needed:

I have 3 lists:

L1 = ["Eagle", "Panther"]

L2 = ["Warrior", "Talon", "Machete"]

L3 = ["Feather", "Raptor", "Hunter", "Piranha"]

The size of the list can vary.

I want to form all subsets {[a,b,c]}such that aat L1, bat L2, catL3

An example is as follows:

{["Eagle", "Warrior", "Feather"], ["Eagle", "Warrior", "Raptor"], ["Panther", "Machete", "Feather"]...}

["Eagle", "Warrior", "Feather"]is the same as ["Eagle", "Feather", "Warrior"], so the order doesn't matter.

I only need all subsets.

I've seen a lot of posts where subsets of lists can be formed, but I can't find what I'm looking for here. I can obviously do this in a loop, but was wondering if there was a itertoolssolution

CDJB

itertools.product is used for this purpose.

import itertools

L1 = ["Eagle", "Panther"]

L2 = ["Warrior", "Talon", "Machete"]

L3 = ["Feather", "Raptor", "Hunter", "Piranha"]

for x in itertools.product(L1,L2,L3):
    print(x)

output

('Eagle', 'Warrior', 'Feather')
('Eagle', 'Warrior', 'Raptor')
('Eagle', 'Warrior', 'Hunter')
('Eagle', 'Warrior', 'Piranha')
('Eagle', 'Talon', 'Feather')
('Eagle', 'Talon', 'Raptor')
('Eagle', 'Talon', 'Hunter')
('Eagle', 'Talon', 'Piranha')
('Eagle', 'Machete', 'Feather')
('Eagle', 'Machete', 'Raptor')
('Eagle', 'Machete', 'Hunter')
('Eagle', 'Machete', 'Piranha')
('Panther', 'Warrior', 'Feather')
('Panther', 'Warrior', 'Raptor')
('Panther', 'Warrior', 'Hunter')
('Panther', 'Warrior', 'Piranha')
('Panther', 'Talon', 'Feather')
('Panther', 'Talon', 'Raptor')
('Panther', 'Talon', 'Hunter')
('Panther', 'Talon', 'Piranha')
('Panther', 'Machete', 'Feather')
('Panther', 'Machete', 'Raptor')
('Panther', 'Machete', 'Hunter')
('Panther', 'Machete', 'Piranha')

Related


How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

count of all possible combinations from multiple lists

JDraper I have the following list: list_a = set(["A", "B", "C", "D", "E", "F"]) list_b = set(["1", "2", "3", "4", "5", "6"]) list_c = set(["red", "yellow", "blue", "green"]) I want to find the total number of possible combinations of these lists (one item p

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

count of all possible combinations from multiple lists

JDraper I have the following list: list_a = set(["A", "B", "C", "D", "E", "F"]) list_b = set(["1", "2", "3", "4", "5", "6"]) list_c = set(["red", "yellow", "blue", "green"]) I want to find the total number of possible combinations of these lists (one item p

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all possible combinations from multiple lists

Merrim Salam, I want to get a list of combinations representing each unique combination of all values in my list of lists. For example, there is a list with a list of sessions, and I want the combination of each session list: My List of lists { List of

Generate all combinations from multiple (n) lists

Anthony Nichols EDIT: I've completely redone my question since I've found the easiest way to ask it. Thanks to the commenters for making me think about the underlying problem so far. public List<string> GetAllPossibleCombos(List<List<string>> strings) { Li

Generate all combinations from multiple (n) lists

Anthony Nichols EDIT: I've completely redone my question since I've found the easiest way to ask it. Thanks to the commenters for making me think about the underlying problem so far. public List<string> GetAllPossibleCombos(List<List<string>> strings) { Li

Generate all combinations from multiple (n) lists

Anthony Nichols EDIT: I've completely redone my question since I've found the easiest way to ask it. Thanks to the commenters for making me think about the underlying problem so far. public List<string> GetAllPossibleCombos(List<List<string>> strings) { Li

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

get all possible combinations of any non-zero length from n lists

Cliodna I have a list that looks like this: [["0"], ["1", "2"], ["4"]] And I want to get all possible non-zero length permutations, taking no more than one element from each of this list, even just the number of permutations. So the result of the above list w

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th