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', '22', '31'],
    ['11', '22', '32'],
    ['12', '21', '31'],
    ['12', '21', '32'],
    ['12', '22', '31'],
    ['12', '22', '32']
]
net wave

user itertools, combination :

import itertools
a = ['11', '12']
b = ['21', '22']
c = ['31', '32']
list(itertools.combinations(itertools.chain(a,b,c), 3))
[('11', '12', '21'), ('11', '12', '22'), ('11', '12', '31'), ('11', '12', '32'), ('11', '21', '22'), ('11', '21', '31'), ('11', '21', '32'), ('11', '22', '31'), ('11', '22', '32'), ('11', '31', '32'), ('12', '21', '22'), ('12', '21', '31'), ('12', '21', '32'), ('12', '22', '31'), ('12', '22', '32'), ('12', '31', '32'), ('21', '22', '31'), ('21', '22', '32'), ('21', '31', '32'), ('22', '31', '32')]

or product :

list(itertools.product(a,b,c))
[('11', '21', '31'), ('11', '21', '32'), ('11', '22', '31'), ('11', '22', '32'), ('12', '21', '31'), ('12', '21', '32'), ('12', '22', '31'), ('12', '22', '32')]

Related


Generate all combinations from multiple lists

Michael Hillman: Given an unknown number of lists, each of unknown length, I need to generate a singular list of all possible unique combinations. For example, given the following list: X: [A, B, C] Y: [W, X, Y, Z] Then, I should be able to generate 12 combi

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple arrays?

Caio Favero Suppose I have these three arrays $array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); I need this output 1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 One of my problems is that my array myght varies from 3 to 15 different arrays,

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 lists

Michael Hillman: Given an unknown number of lists, each of unknown length, I need to generate a singular list of all possible unique combinations. For example, given the following list: X: [A, B, C] Y: [W, X, Y, Z] Then, I should be able to generate 12 combi

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

How to get all combinations from multiple arrays?

Caio Favero Suppose I have these 3 arrays $array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); I need this output 1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 One of my problems is that my array myght varies from 3 to 15 different arrays, eac

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

How to get all combinations of "sub"lists from a list

Wow So if I start with: x = [a, b, c] y = [[a], [b], [c], [a,b], [b,c]] How can I get all combinations xof elements from it y? It's just like: y = [ [[a, b], c], [[b,c], a], [a, b, c] I've looked into itertools and list comprehensions but still struggling. A

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple arrays?

Caio Favero Suppose I have these three arrays $array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); I need this output 1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 One of my problems is that my array myght varies from 3 to 15 different arrays,

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

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

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'

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

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple arrays?

Caio Favero Suppose I have these 3 arrays $array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); I need this output 1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 One of my problems is that my array myght varies from 3 to 15 different arrays, eac

Generate all combinations from multiple lists

Michael Hillman: Given an unknown number of lists, each of unknown length, I need to generate a singular list of all possible unique combinations. For example, given the following list: X: [A, B, C] Y: [W, X, Y, Z] Then, I should be able to generate 12 combi

Generate all combinations from multiple lists

Michael Hillman: Given an unknown number of lists, each of unknown length, I need to generate a singular list of all possible unique combinations. For example, given the following list: X: [A, B, C] Y: [W, X, Y, Z] Then, I should be able to generate 12 combi

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

How to get all combinations from multiple arrays?

Caio Favero Suppose I have these three arrays $array1 = array(1,2); $array2 = array(4,5); $array3 = array(7,8); I need this output 1 4 7 1 4 8 1 5 7 1 5 8 2 4 7 2 4 8 2 5 7 2 5 8 One of my problems is that my array myght varies from 3 to 15 different arrays,

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'

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

How to get all combinations of "sub"lists from a list

Wow So if I start with: x = [a, b, c] y = [[a], [b], [c], [a,b], [b,c]] How can I get all combinations xof elements from it y? It's just like: y = [ [[a, b], c], [[b,c], a], [a, b, c] I've looked into itertools and list comprehensions but still struggling. A