Combinations of multiple listsHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to return multiple values from a function?How to make a flat list out of list of lists?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?Catch multiple exceptions in one line (except block)
Doing something right before you need it - expression for this?
What is the word for reserving something for yourself before others do?
What mechanic is there to disable a threat instead of killing it?
Why does Kotter return in Welcome Back Kotter
Fully-Firstable Anagram Sets
AES: Why is it a good practice to use only the first 16bytes of a hash for encryption?
Western buddy movie with a supernatural twist where a woman turns into an eagle at the end
Why does Arabsat 6A need a Falcon Heavy to launch
Did converts (ger tzedek) in ancient Israel own land?
Why doesn't H₄O²⁺ exist?
What's the difference between 'rename' and 'mv'?
Can a virus destroy the BIOS of a modern computer?
Blender 2.8 I can't see vertices, edges or faces in edit mode
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
How to model explosives?
In a Spin are Both Wings Stalled?
Etiquette around loan refinance - decision is going to cost first broker a lot of money
Is "remove commented out code" correct English?
Can I ask the recruiters in my resume to put the reason why I am rejected?
Why is Collection not simply treated as Collection<?>
What is the intuition behind short exact sequences of groups; in particular, what is the intuition behind group extensions?
What is the PIE reconstruction for word-initial alpha with rough breathing?
How to draw the figure with four pentagons?
Combinations of multiple lists
How do I check if a list is empty?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow to return multiple values from a function?How to make a flat list out of list of lists?How do I concatenate two lists in Python?How to clone or copy a list?How do I list all files of a directory?How to read a file line-by-line into a list?Catch multiple exceptions in one line (except block)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Suppose I have three lists:
list1 --> [a, b, c, d, e, f, g, h]
list2 --> [i, j, k]
list3 --> [l, m, n, o, p]
I wish to generate all combinations where I take five elements from list1, two elements from list2 and three elements from list3.
eg.
a, b, c, d, e, i, j, l, m, n
a, b, c, d, e, i, j, l, m, o
etc.
I tried to use itertools.combinations.
l1_combinations = itertools.combinations(list1, 5)
l2_combinations = itertools.combinations(list2, 2)
l3_combinations = itertools.combinations(list3, 3)
for l1_iterator in list(l1_combinations):
for l2_iterator in list(l2_combinations): #added a missing )
for l3_iterator in list(l3_combinations):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
But I am getting output with iterations happening only on list3. In all the output, only first five elements from list1 and first two elements from list2 are present. Combinations with other elements from those two lists aren't present.
Can someone help me here and also explain what exactly did i miss ?
python
add a comment |
Suppose I have three lists:
list1 --> [a, b, c, d, e, f, g, h]
list2 --> [i, j, k]
list3 --> [l, m, n, o, p]
I wish to generate all combinations where I take five elements from list1, two elements from list2 and three elements from list3.
eg.
a, b, c, d, e, i, j, l, m, n
a, b, c, d, e, i, j, l, m, o
etc.
I tried to use itertools.combinations.
l1_combinations = itertools.combinations(list1, 5)
l2_combinations = itertools.combinations(list2, 2)
l3_combinations = itertools.combinations(list3, 3)
for l1_iterator in list(l1_combinations):
for l2_iterator in list(l2_combinations): #added a missing )
for l3_iterator in list(l3_combinations):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
But I am getting output with iterations happening only on list3. In all the output, only first five elements from list1 and first two elements from list2 are present. Combinations with other elements from those two lists aren't present.
Can someone help me here and also explain what exactly did i miss ?
python
Well I'm sure the missing)
infor l2_iterator in list(l2_combinations:
doesn't help...
– Reedinationer
5 hours ago
add a comment |
Suppose I have three lists:
list1 --> [a, b, c, d, e, f, g, h]
list2 --> [i, j, k]
list3 --> [l, m, n, o, p]
I wish to generate all combinations where I take five elements from list1, two elements from list2 and three elements from list3.
eg.
a, b, c, d, e, i, j, l, m, n
a, b, c, d, e, i, j, l, m, o
etc.
I tried to use itertools.combinations.
l1_combinations = itertools.combinations(list1, 5)
l2_combinations = itertools.combinations(list2, 2)
l3_combinations = itertools.combinations(list3, 3)
for l1_iterator in list(l1_combinations):
for l2_iterator in list(l2_combinations): #added a missing )
for l3_iterator in list(l3_combinations):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
But I am getting output with iterations happening only on list3. In all the output, only first five elements from list1 and first two elements from list2 are present. Combinations with other elements from those two lists aren't present.
Can someone help me here and also explain what exactly did i miss ?
python
Suppose I have three lists:
list1 --> [a, b, c, d, e, f, g, h]
list2 --> [i, j, k]
list3 --> [l, m, n, o, p]
I wish to generate all combinations where I take five elements from list1, two elements from list2 and three elements from list3.
eg.
a, b, c, d, e, i, j, l, m, n
a, b, c, d, e, i, j, l, m, o
etc.
I tried to use itertools.combinations.
l1_combinations = itertools.combinations(list1, 5)
l2_combinations = itertools.combinations(list2, 2)
l3_combinations = itertools.combinations(list3, 3)
for l1_iterator in list(l1_combinations):
for l2_iterator in list(l2_combinations): #added a missing )
for l3_iterator in list(l3_combinations):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
But I am getting output with iterations happening only on list3. In all the output, only first five elements from list1 and first two elements from list2 are present. Combinations with other elements from those two lists aren't present.
Can someone help me here and also explain what exactly did i miss ?
python
python
edited 5 hours ago
amchugh89
595621
595621
asked 5 hours ago
Arun AranganathanArun Aranganathan
475
475
Well I'm sure the missing)
infor l2_iterator in list(l2_combinations:
doesn't help...
– Reedinationer
5 hours ago
add a comment |
Well I'm sure the missing)
infor l2_iterator in list(l2_combinations:
doesn't help...
– Reedinationer
5 hours ago
Well I'm sure the missing
)
in for l2_iterator in list(l2_combinations:
doesn't help...– Reedinationer
5 hours ago
Well I'm sure the missing
)
in for l2_iterator in list(l2_combinations:
doesn't help...– Reedinationer
5 hours ago
add a comment |
2 Answers
2
active
oldest
votes
As an alternative to regenerating the list of combinations, compute the product of the combinations up front; this also saves you from nesting for
loops.
from itertools import combinations, product
list1 = list("abcdefgh")
list2 = list("ijk")
list3 = list("lmnop")
l1 = combinations(list1, 5)
l2 = combinations(list2, 2)
l3 = combinations(list3, 3)
for c1, c2, c3 in product(l1, l2, l3):
sample = c1 + c2 + c3
print(sample)
add a comment |
Don't iterate over the same iterator multiple times, after the first time it's exhausted. Iterate over a fresh iterator each time:
for l1_iterator in itertools.combinations(list1, 5):
for l2_iterator in itertools.combinations(list2, 2):
for l3_iterator in itertools.combinations(list3, 3):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
Or make lists of each one in advance to avoid recomputation:
l1_combinations = list(itertools.combinations(list1, 5))
l2_combinations = list(itertools.combinations(list2, 2))
l3_combinations = list(itertools.combinations(list3, 3))
for l1_iterator in l1_combinations:
for l2_iterator in l2_combinations:
for l3_iterator in l3_combinations:
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
2
@ritlew see edit.
– Alex Hall
5 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55523811%2fcombinations-of-multiple-lists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As an alternative to regenerating the list of combinations, compute the product of the combinations up front; this also saves you from nesting for
loops.
from itertools import combinations, product
list1 = list("abcdefgh")
list2 = list("ijk")
list3 = list("lmnop")
l1 = combinations(list1, 5)
l2 = combinations(list2, 2)
l3 = combinations(list3, 3)
for c1, c2, c3 in product(l1, l2, l3):
sample = c1 + c2 + c3
print(sample)
add a comment |
As an alternative to regenerating the list of combinations, compute the product of the combinations up front; this also saves you from nesting for
loops.
from itertools import combinations, product
list1 = list("abcdefgh")
list2 = list("ijk")
list3 = list("lmnop")
l1 = combinations(list1, 5)
l2 = combinations(list2, 2)
l3 = combinations(list3, 3)
for c1, c2, c3 in product(l1, l2, l3):
sample = c1 + c2 + c3
print(sample)
add a comment |
As an alternative to regenerating the list of combinations, compute the product of the combinations up front; this also saves you from nesting for
loops.
from itertools import combinations, product
list1 = list("abcdefgh")
list2 = list("ijk")
list3 = list("lmnop")
l1 = combinations(list1, 5)
l2 = combinations(list2, 2)
l3 = combinations(list3, 3)
for c1, c2, c3 in product(l1, l2, l3):
sample = c1 + c2 + c3
print(sample)
As an alternative to regenerating the list of combinations, compute the product of the combinations up front; this also saves you from nesting for
loops.
from itertools import combinations, product
list1 = list("abcdefgh")
list2 = list("ijk")
list3 = list("lmnop")
l1 = combinations(list1, 5)
l2 = combinations(list2, 2)
l3 = combinations(list3, 3)
for c1, c2, c3 in product(l1, l2, l3):
sample = c1 + c2 + c3
print(sample)
answered 5 hours ago
chepnerchepner
261k34250343
261k34250343
add a comment |
add a comment |
Don't iterate over the same iterator multiple times, after the first time it's exhausted. Iterate over a fresh iterator each time:
for l1_iterator in itertools.combinations(list1, 5):
for l2_iterator in itertools.combinations(list2, 2):
for l3_iterator in itertools.combinations(list3, 3):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
Or make lists of each one in advance to avoid recomputation:
l1_combinations = list(itertools.combinations(list1, 5))
l2_combinations = list(itertools.combinations(list2, 2))
l3_combinations = list(itertools.combinations(list3, 3))
for l1_iterator in l1_combinations:
for l2_iterator in l2_combinations:
for l3_iterator in l3_combinations:
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
2
@ritlew see edit.
– Alex Hall
5 hours ago
add a comment |
Don't iterate over the same iterator multiple times, after the first time it's exhausted. Iterate over a fresh iterator each time:
for l1_iterator in itertools.combinations(list1, 5):
for l2_iterator in itertools.combinations(list2, 2):
for l3_iterator in itertools.combinations(list3, 3):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
Or make lists of each one in advance to avoid recomputation:
l1_combinations = list(itertools.combinations(list1, 5))
l2_combinations = list(itertools.combinations(list2, 2))
l3_combinations = list(itertools.combinations(list3, 3))
for l1_iterator in l1_combinations:
for l2_iterator in l2_combinations:
for l3_iterator in l3_combinations:
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
2
@ritlew see edit.
– Alex Hall
5 hours ago
add a comment |
Don't iterate over the same iterator multiple times, after the first time it's exhausted. Iterate over a fresh iterator each time:
for l1_iterator in itertools.combinations(list1, 5):
for l2_iterator in itertools.combinations(list2, 2):
for l3_iterator in itertools.combinations(list3, 3):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
Or make lists of each one in advance to avoid recomputation:
l1_combinations = list(itertools.combinations(list1, 5))
l2_combinations = list(itertools.combinations(list2, 2))
l3_combinations = list(itertools.combinations(list3, 3))
for l1_iterator in l1_combinations:
for l2_iterator in l2_combinations:
for l3_iterator in l3_combinations:
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
Don't iterate over the same iterator multiple times, after the first time it's exhausted. Iterate over a fresh iterator each time:
for l1_iterator in itertools.combinations(list1, 5):
for l2_iterator in itertools.combinations(list2, 2):
for l3_iterator in itertools.combinations(list3, 3):
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
Or make lists of each one in advance to avoid recomputation:
l1_combinations = list(itertools.combinations(list1, 5))
l2_combinations = list(itertools.combinations(list2, 2))
l3_combinations = list(itertools.combinations(list3, 3))
for l1_iterator in l1_combinations:
for l2_iterator in l2_combinations:
for l3_iterator in l3_combinations:
sample = l1_iterator + l2_iterator + l3_iterator
print(sample)
edited 5 hours ago
answered 5 hours ago
Alex HallAlex Hall
23k32053
23k32053
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
2
@ritlew see edit.
– Alex Hall
5 hours ago
add a comment |
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
2
@ritlew see edit.
– Alex Hall
5 hours ago
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
What would be a solution to not regenerate the combinations every time?
– ritlew
5 hours ago
2
2
@ritlew see edit.
– Alex Hall
5 hours ago
@ritlew see edit.
– Alex Hall
5 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55523811%2fcombinations-of-multiple-lists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Well I'm sure the missing
)
infor l2_iterator in list(l2_combinations:
doesn't help...– Reedinationer
5 hours ago