Unique combinations of a list of tuplesPicking unordered combinations from pools with overlapHow do I check...
How do we avoid CI-driven development...?
Double blind peer review when paper cites author's GitHub repo for code
Acceptable to cut steak before searing?
How do I calculate the difference in lens reach between a superzoom compact and a DSLR zoom lens?
Could one become a successful researcher by writing some really good papers while being outside academia?
How many different ways are there to checkmate in the early game?
sed delete all the words before a match
What's this thing in a peltier cooler?
Can I legally make a real mobile app based on a fictional app from a TV show?
Team goes to lunch frequently, I do intermittent fasting but still want to socialize
What are the uses and limitations of Persuasion, Insight, and Deception against other PCs?
What are these two charactes marked red
Ordering a word list
Unique combinations of a list of tuples
Best way to divide CPU along all sql instances on 1 server
What is the maximum number of PC-controlled undead?
What does Apple mean by "This may decrease battery life"?
How to mark beverage cans in a cooler for a blind person?
Generator for parity?
In Pokémon Go, why does one of my Pikachu have an option to evolve, but another one doesn't?
Author changing name
What does "sardine box" mean?
Is it really ~648.69 km/s delta-v to "land" on the surface of the Sun?
Are there any financial disadvantages to living significantly "below your means"?
Unique combinations of a list of tuples
Picking unordered combinations from pools with overlapHow do I check if a list is empty?Finding the index of an item given a list containing it in PythonWhat is the difference between Python's list methods append and extend?What's the difference between lists and tuples?How to make a flat list out of list of listsHow do I concatenate two lists in Python?How to clone or copy a list?What are “named tuples” in Python?How to sort (list/tuple) of lists/tuples by the element at a given index?How do I list all files of a directory?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)]
how would you compute all possible combinations and combinations of subsets?
In this case the result should look like this:
[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]
- all tuples with identical elements are regarded the same
- combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution:
(1,2)
,(4,6)
or(7,8,9)
)
python tuples combinations permutation
add a comment |
Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)]
how would you compute all possible combinations and combinations of subsets?
In this case the result should look like this:
[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]
- all tuples with identical elements are regarded the same
- combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution:
(1,2)
,(4,6)
or(7,8,9)
)
python tuples combinations permutation
But wait, why(1)
to(9)
are part of the soultion if(1,2)
is not allowed given the second rule ?
– Drey
8 hours ago
It looks like there are three sets of tuples: 1)[(x,) for x in the_list[0]]
, 2)[(x,y) for x in the_list[0] for y in the_list[1]]
, and 3)[(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]]
.
– chepner
8 hours ago
Possible duplicate of Picking unordered combinations from pools with overlap
– Joseph Wood
8 hours ago
add a comment |
Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)]
how would you compute all possible combinations and combinations of subsets?
In this case the result should look like this:
[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]
- all tuples with identical elements are regarded the same
- combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution:
(1,2)
,(4,6)
or(7,8,9)
)
python tuples combinations permutation
Given a list of 3-tuples, for example:[(1,2,3), (4,5,6), (7,8,9)]
how would you compute all possible combinations and combinations of subsets?
In this case the result should look like this:
[
(1), (1,4), (1,5), (1,6), (1,7), (1,8), (1,9), (1,4,7), (1,4,8), (1,4,9), (1,5,7), (1,5,8), (1,5,9), (1,6,7), (1,6,8), (1,6,9),
(2), ...,
(3), ...,
(4), (4,7), (4,8), (4,9),
(5), (5,7), (5,8), (5,9),
(6), (6,7), (6,8), (6,9),
(7), (8), (9)
]
- all tuples with identical elements are regarded the same
- combinations which derive from the same tuples are not allowed (e.g. these shouldn't be in the solution:
(1,2)
,(4,6)
or(7,8,9)
)
python tuples combinations permutation
python tuples combinations permutation
asked 9 hours ago
p4rchp4rch
626 bronze badges
626 bronze badges
But wait, why(1)
to(9)
are part of the soultion if(1,2)
is not allowed given the second rule ?
– Drey
8 hours ago
It looks like there are three sets of tuples: 1)[(x,) for x in the_list[0]]
, 2)[(x,y) for x in the_list[0] for y in the_list[1]]
, and 3)[(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]]
.
– chepner
8 hours ago
Possible duplicate of Picking unordered combinations from pools with overlap
– Joseph Wood
8 hours ago
add a comment |
But wait, why(1)
to(9)
are part of the soultion if(1,2)
is not allowed given the second rule ?
– Drey
8 hours ago
It looks like there are three sets of tuples: 1)[(x,) for x in the_list[0]]
, 2)[(x,y) for x in the_list[0] for y in the_list[1]]
, and 3)[(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]]
.
– chepner
8 hours ago
Possible duplicate of Picking unordered combinations from pools with overlap
– Joseph Wood
8 hours ago
But wait, why
(1)
to (9)
are part of the soultion if (1,2)
is not allowed given the second rule ?– Drey
8 hours ago
But wait, why
(1)
to (9)
are part of the soultion if (1,2)
is not allowed given the second rule ?– Drey
8 hours ago
It looks like there are three sets of tuples: 1)
[(x,) for x in the_list[0]]
, 2) [(x,y) for x in the_list[0] for y in the_list[1]]
, and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]]
.– chepner
8 hours ago
It looks like there are three sets of tuples: 1)
[(x,) for x in the_list[0]]
, 2) [(x,y) for x in the_list[0] for y in the_list[1]]
, and 3) [(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]]
.– chepner
8 hours ago
Possible duplicate of Picking unordered combinations from pools with overlap
– Joseph Wood
8 hours ago
Possible duplicate of Picking unordered combinations from pools with overlap
– Joseph Wood
8 hours ago
add a comment |
5 Answers
5
active
oldest
votes
You can use recursion with a generator:
data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
if len(c) == len(d):
yield c
else:
for i in d:
if i not in c:
yield from combos(d, c+[i])
def product(d, c = []):
if c:
yield tuple(c)
if d:
for i in d[0]:
yield from product(d[1:], c+[i])
result = sorted({i for b in combos(data) for i in product(b)})
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]
Output:
[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
add a comment |
Using itertools:
import itertools as it
def all_combinations(groups):
result = set()
for prod in it.product(*groups):
for length in range(1, len(groups) + 1):
result.update(it.combinations(prod, length))
return result
all_combinations([(1,2,3), (4,5,6), (7,8,9)])
add a comment |
Here is a non-recursive solution with a simple for
loop. Uniqueness if enforced by applying set
to the list of output tuples.
lsts = [(1,2,3), (4,5,6), (7,8,9)]
res = [[]]
for lst in lsts:
res += [(*r, x) for r in res for x in lst]
# print({tuple(lst) for lst in res[1:]})
# {(5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
# 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
# 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
# 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
# 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
# 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
# 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
# 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
# 4), (2, 4)}
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
add a comment |
Another version:
from itertools import product
lst = [(1,2,3), (4,5,6), (7,8,9)]
def generate(lst):
for idx in range(len(lst)):
for val in lst[idx]:
yield (val,)
for i in range(len(lst), idx+1, -1):
l = tuple((val,) + i for i in product(*lst[idx+1:i]))
if len(l) > 1:
yield from l
l = [*generate(lst)]
print(l)
Prints:
[(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
add a comment |
Oookay, although I don't fully understand the second rule, here is some short solution to the problem
from itertools import chain, combinations
blubb = [(1,2,3), (4,5,6), (7,8,9)]
blubb_as_set = list(map(set, blubb))
all_blubbs = list(chain.from_iterable(blubb))
all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
as_a_list = list(chain.from_iterable(all_blubb_combos))
test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
list(filter(test_subset, as_a_list))
# alternative that includes single elements
list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))
For the most parts you can leave out list
calls.
Your can also create different not_allowed
cases based on r
if you need to deal with tuple's length more than 3.
Edit: explicit test for subset.
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');
});
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%2f57444657%2funique-combinations-of-a-list-of-tuples%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use recursion with a generator:
data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
if len(c) == len(d):
yield c
else:
for i in d:
if i not in c:
yield from combos(d, c+[i])
def product(d, c = []):
if c:
yield tuple(c)
if d:
for i in d[0]:
yield from product(d[1:], c+[i])
result = sorted({i for b in combos(data) for i in product(b)})
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]
Output:
[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
add a comment |
You can use recursion with a generator:
data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
if len(c) == len(d):
yield c
else:
for i in d:
if i not in c:
yield from combos(d, c+[i])
def product(d, c = []):
if c:
yield tuple(c)
if d:
for i in d[0]:
yield from product(d[1:], c+[i])
result = sorted({i for b in combos(data) for i in product(b)})
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]
Output:
[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
add a comment |
You can use recursion with a generator:
data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
if len(c) == len(d):
yield c
else:
for i in d:
if i not in c:
yield from combos(d, c+[i])
def product(d, c = []):
if c:
yield tuple(c)
if d:
for i in d[0]:
yield from product(d[1:], c+[i])
result = sorted({i for b in combos(data) for i in product(b)})
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]
Output:
[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
You can use recursion with a generator:
data = [(1,2,3), (4,5,6), (7,8,9)]
def combos(d, c = []):
if len(c) == len(d):
yield c
else:
for i in d:
if i not in c:
yield from combos(d, c+[i])
def product(d, c = []):
if c:
yield tuple(c)
if d:
for i in d[0]:
yield from product(d[1:], c+[i])
result = sorted({i for b in combos(data) for i in product(b)})
final_result = [a for i, a in enumerate(result) if all(len(c) != len(a) or len(set(c)&set(a)) != len(a) for c in result[:i])]
Output:
[(1,), (1, 4), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6), (1, 6, 7), (1, 6, 8), (1, 6, 9), (1, 7), (1, 8), (1, 9), (2,), (2, 4), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6), (2, 6, 7), (2, 6, 8), (2, 6, 9), (2, 7), (2, 8), (2, 9), (3,), (3, 4), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6), (3, 6, 7), (3, 6, 8), (3, 6, 9), (3, 7), (3, 8), (3, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
edited 9 hours ago
answered 9 hours ago
Ajax1234Ajax1234
46.6k4 gold badges31 silver badges60 bronze badges
46.6k4 gold badges31 silver badges60 bronze badges
add a comment |
add a comment |
Using itertools:
import itertools as it
def all_combinations(groups):
result = set()
for prod in it.product(*groups):
for length in range(1, len(groups) + 1):
result.update(it.combinations(prod, length))
return result
all_combinations([(1,2,3), (4,5,6), (7,8,9)])
add a comment |
Using itertools:
import itertools as it
def all_combinations(groups):
result = set()
for prod in it.product(*groups):
for length in range(1, len(groups) + 1):
result.update(it.combinations(prod, length))
return result
all_combinations([(1,2,3), (4,5,6), (7,8,9)])
add a comment |
Using itertools:
import itertools as it
def all_combinations(groups):
result = set()
for prod in it.product(*groups):
for length in range(1, len(groups) + 1):
result.update(it.combinations(prod, length))
return result
all_combinations([(1,2,3), (4,5,6), (7,8,9)])
Using itertools:
import itertools as it
def all_combinations(groups):
result = set()
for prod in it.product(*groups):
for length in range(1, len(groups) + 1):
result.update(it.combinations(prod, length))
return result
all_combinations([(1,2,3), (4,5,6), (7,8,9)])
edited 9 hours ago
answered 9 hours ago
eumiroeumiro
139k22 gold badges245 silver badges237 bronze badges
139k22 gold badges245 silver badges237 bronze badges
add a comment |
add a comment |
Here is a non-recursive solution with a simple for
loop. Uniqueness if enforced by applying set
to the list of output tuples.
lsts = [(1,2,3), (4,5,6), (7,8,9)]
res = [[]]
for lst in lsts:
res += [(*r, x) for r in res for x in lst]
# print({tuple(lst) for lst in res[1:]})
# {(5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
# 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
# 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
# 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
# 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
# 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
# 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
# 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
# 4), (2, 4)}
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
add a comment |
Here is a non-recursive solution with a simple for
loop. Uniqueness if enforced by applying set
to the list of output tuples.
lsts = [(1,2,3), (4,5,6), (7,8,9)]
res = [[]]
for lst in lsts:
res += [(*r, x) for r in res for x in lst]
# print({tuple(lst) for lst in res[1:]})
# {(5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
# 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
# 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
# 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
# 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
# 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
# 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
# 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
# 4), (2, 4)}
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
add a comment |
Here is a non-recursive solution with a simple for
loop. Uniqueness if enforced by applying set
to the list of output tuples.
lsts = [(1,2,3), (4,5,6), (7,8,9)]
res = [[]]
for lst in lsts:
res += [(*r, x) for r in res for x in lst]
# print({tuple(lst) for lst in res[1:]})
# {(5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
# 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
# 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
# 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
# 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
# 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
# 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
# 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
# 4), (2, 4)}
Here is a non-recursive solution with a simple for
loop. Uniqueness if enforced by applying set
to the list of output tuples.
lsts = [(1,2,3), (4,5,6), (7,8,9)]
res = [[]]
for lst in lsts:
res += [(*r, x) for r in res for x in lst]
# print({tuple(lst) for lst in res[1:]})
# {(5, 9), (4, 7), (6, 9), (1, 4, 7), (2, 6, 9), (4, 8), (3, 4, 7), (2,
# 8), (2, 6, 8), (9,), (2, 5, 8), (1, 6), (3, 6, 8), (2, 5, 9), (3, 5,
# 9), (3, 7), (2, 5), (3, 6, 9), (5, 8), (1, 6, 8), (3, 5, 8), (2, 6,
# 7), (4, 9), (6, 7), (1,), (2, 9), (1, 6, 9), (3,), (1, 5), (5,), (3,
# 6), (7,), (3, 6, 7), (1, 5, 9), (2, 6), (2, 4, 7), (1, 5, 8), (3, 4,
# 8), (8,), (3, 4, 9), (1, 4), (1, 6, 7), (3, 9), (1, 9), (2, 5, 7), (3,
# 5), (2, 7), (2, 4, 9), (6, 8), (1, 5, 7), (2,), (2, 4, 8), (5, 7), (1,
# 4, 8), (3, 5, 7), (4,), (3, 8), (1, 8), (1, 4, 9), (6,), (1, 7), (3,
# 4), (2, 4)}
answered 7 hours ago
hilberts_drinking_problemhilberts_drinking_problem
6,7713 gold badges14 silver badges32 bronze badges
6,7713 gold badges14 silver badges32 bronze badges
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
add a comment |
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Nice! But don't forget to add singleton solutions (as per strange exception from rule 2)
– Drey
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Those are in the set, just hard to see :)
– hilberts_drinking_problem
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
Ups, yes, my bad, now I see them :D
– Drey
7 hours ago
add a comment |
Another version:
from itertools import product
lst = [(1,2,3), (4,5,6), (7,8,9)]
def generate(lst):
for idx in range(len(lst)):
for val in lst[idx]:
yield (val,)
for i in range(len(lst), idx+1, -1):
l = tuple((val,) + i for i in product(*lst[idx+1:i]))
if len(l) > 1:
yield from l
l = [*generate(lst)]
print(l)
Prints:
[(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
add a comment |
Another version:
from itertools import product
lst = [(1,2,3), (4,5,6), (7,8,9)]
def generate(lst):
for idx in range(len(lst)):
for val in lst[idx]:
yield (val,)
for i in range(len(lst), idx+1, -1):
l = tuple((val,) + i for i in product(*lst[idx+1:i]))
if len(l) > 1:
yield from l
l = [*generate(lst)]
print(l)
Prints:
[(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
add a comment |
Another version:
from itertools import product
lst = [(1,2,3), (4,5,6), (7,8,9)]
def generate(lst):
for idx in range(len(lst)):
for val in lst[idx]:
yield (val,)
for i in range(len(lst), idx+1, -1):
l = tuple((val,) + i for i in product(*lst[idx+1:i]))
if len(l) > 1:
yield from l
l = [*generate(lst)]
print(l)
Prints:
[(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
Another version:
from itertools import product
lst = [(1,2,3), (4,5,6), (7,8,9)]
def generate(lst):
for idx in range(len(lst)):
for val in lst[idx]:
yield (val,)
for i in range(len(lst), idx+1, -1):
l = tuple((val,) + i for i in product(*lst[idx+1:i]))
if len(l) > 1:
yield from l
l = [*generate(lst)]
print(l)
Prints:
[(1,), (1, 4), (1, 5), (1, 6), (1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2,), (2, 4), (2, 5), (2, 6), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5, 8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3,), (3, 4), (3, 5), (3, 6), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9), (4,), (4, 7), (4, 8), (4, 9), (5,), (5, 7), (5, 8), (5, 9), (6,), (6, 7), (6, 8), (6, 9), (7,), (8,), (9,)]
edited 9 hours ago
answered 9 hours ago
Andrej KeselyAndrej Kesely
17.9k2 gold badges10 silver badges34 bronze badges
17.9k2 gold badges10 silver badges34 bronze badges
add a comment |
add a comment |
Oookay, although I don't fully understand the second rule, here is some short solution to the problem
from itertools import chain, combinations
blubb = [(1,2,3), (4,5,6), (7,8,9)]
blubb_as_set = list(map(set, blubb))
all_blubbs = list(chain.from_iterable(blubb))
all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
as_a_list = list(chain.from_iterable(all_blubb_combos))
test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
list(filter(test_subset, as_a_list))
# alternative that includes single elements
list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))
For the most parts you can leave out list
calls.
Your can also create different not_allowed
cases based on r
if you need to deal with tuple's length more than 3.
Edit: explicit test for subset.
add a comment |
Oookay, although I don't fully understand the second rule, here is some short solution to the problem
from itertools import chain, combinations
blubb = [(1,2,3), (4,5,6), (7,8,9)]
blubb_as_set = list(map(set, blubb))
all_blubbs = list(chain.from_iterable(blubb))
all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
as_a_list = list(chain.from_iterable(all_blubb_combos))
test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
list(filter(test_subset, as_a_list))
# alternative that includes single elements
list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))
For the most parts you can leave out list
calls.
Your can also create different not_allowed
cases based on r
if you need to deal with tuple's length more than 3.
Edit: explicit test for subset.
add a comment |
Oookay, although I don't fully understand the second rule, here is some short solution to the problem
from itertools import chain, combinations
blubb = [(1,2,3), (4,5,6), (7,8,9)]
blubb_as_set = list(map(set, blubb))
all_blubbs = list(chain.from_iterable(blubb))
all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
as_a_list = list(chain.from_iterable(all_blubb_combos))
test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
list(filter(test_subset, as_a_list))
# alternative that includes single elements
list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))
For the most parts you can leave out list
calls.
Your can also create different not_allowed
cases based on r
if you need to deal with tuple's length more than 3.
Edit: explicit test for subset.
Oookay, although I don't fully understand the second rule, here is some short solution to the problem
from itertools import chain, combinations
blubb = [(1,2,3), (4,5,6), (7,8,9)]
blubb_as_set = list(map(set, blubb))
all_blubbs = list(chain.from_iterable(blubb))
all_blubb_combos = (combinations(all_blubbs, i) for i in range(1, 4))
as_a_list = list(chain.from_iterable(all_blubb_combos))
test_subset = lambda x: not any(set(x).issubset(blubb_set) for blubb_set in blubb_as_set)
list(filter(test_subset, as_a_list))
# alternative that includes single elements
list(filter(test_subset, as_a_list)) + list(map(lambda x: (x, ), chain.from_iterable(blubb)))
For the most parts you can leave out list
calls.
Your can also create different not_allowed
cases based on r
if you need to deal with tuple's length more than 3.
Edit: explicit test for subset.
edited 8 hours ago
answered 8 hours ago
DreyDrey
1,97714 silver badges18 bronze badges
1,97714 silver badges18 bronze badges
add a comment |
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');
});
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%2f57444657%2funique-combinations-of-a-list-of-tuples%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');
});
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');
});
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');
});
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
But wait, why
(1)
to(9)
are part of the soultion if(1,2)
is not allowed given the second rule ?– Drey
8 hours ago
It looks like there are three sets of tuples: 1)
[(x,) for x in the_list[0]]
, 2)[(x,y) for x in the_list[0] for y in the_list[1]]
, and 3)[(x,y,z) for x in the_list[0] for y in the_list[1] for z in the_list[2]]
.– chepner
8 hours ago
Possible duplicate of Picking unordered combinations from pools with overlap
– Joseph Wood
8 hours ago