T
T
TomasHuk2016-01-14 17:07:28
Python
TomasHuk, 2016-01-14 17:07:28

How to generate the following list of lists from numbers?

In general, this is the situation.
There is a list of numbers, for example:
five_group = [3, 5, 13, 18, 20]
You need to generate a list of all combinations of 5 numbers out of 25, each of which contains at least one of the numbers with five_group.
The end result should be a list like this:

LIST = 

I can generate a list of lists from all combinations with
COMBINATIONS = [list(i) for i in itertools.combinations(range(1, 26), 5)]

But how to remove from it combinations in which there are no numbers from five_group?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-01-14
@TomasHuk

each of which contains at least one of the numbers with five_group.

That is, in fact, this is the operation of the intersection of sets.
five_group = set([3, 5, 13, 18, 20])
COMBINATIONS = [list(i) for i in itertools.combinations(
    range(1, 26), 5) if five_group.intersection(i)]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question