1

I want to write a code (in python 3) that is able to calculate the sum of all possible combinations of a varying number of lists. The result of the sum needs to be checked with a specified value. For all combinations where the sum adds up to the specified value, I would like to create a new list containing just those values.

For example:

value = 5
a = [1, 2, 3, 4]
b = [2, 3, 4, 5]

1 + 2 = 3 - x
1 + 3 = 4 - x
1 + 4 = 5 - correct
1 + 5 = 6 - x
2 + 2 = 4 - x
2 + 3 = 5 - correct
...

The result should be for example:

res = [[1, 4], [2, 3], [3, 2], [4, 1]]

I know that a simple option would be to use nested for-loops. The problem is that at the time of writing the code I dont know how many lists there will be, resulting in the need to define all possible cases. This is something I dont want to do. By the time I am running the code, I do know how many lists there are. The length of the lists will always be the same (26 elements).

The lists that need to be checked are stored in a list in the following way. For example:

list = [[1, 2, 3, 4], [2, 3, 4, 5]]

An example of an actual set of lists that I would like to solve this problem for is:

list = [[0, 2, 0, 0, 5, 0, 0, 8, 0, 0, 11, 0, 0, 14, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 5, 0, 0, 8, 0, 0, 11, 0, 0, 14, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

All the zero values are the result of other refinements in the total number of options that do not meet other criterias.

I hope someone can push me into the right direction. Thanks in advance!

5
  • welcome to stackoverflow! please read up on how to ask a question and provide a minimal, complete and verifiable example that reproduces your problem. Commented Dec 30, 2016 at 15:44
  • this may give you a few ideas on how to iterate over combinations of elements in lists: docs.python.org/3/library/itertools.html Commented Dec 30, 2016 at 15:45
  • 2
    What you describe as "all possible combinations of multiple lists" is called the Cartesian product, and you can get it with itertools.product. Commented Dec 30, 2016 at 15:53
  • 1
    Also, your result should be res = [[1, 4], [2, 3], [3, 2]], since there's no 1 in list b. Commented Dec 30, 2016 at 15:54
  • I would also look at comprehensions, for list or set to help build your results, they support an if clause which would help filter out the results to the ones you want. Commented Dec 30, 2016 at 16:08

1 Answer 1

3

With some list of lists l (don't name something list, there's a built in function named list)

l = [[1, 2, 3, 4], [2, 3, 4, 5]]

We can use itertools.product to get all combinations of items between the lists, then map the sum function onto those combinations. Then checking for membership is easy.

from itertools import product

if value in map(sum, product(*l)):
    print('Yes!')
else:
    print('No :(')

If you want to save the sums for multiple checks, I recommend saving them into a set

sum_set = set(map(sum, product(*l)))
if value in sum_set:
    ...

The * in product(*l) takes the is the unpacking operator. It gives the elements of l to product as individual arguments product([1,2,3,4], [2,3,4,5])

Sign up to request clarification or add additional context in comments.

4 Comments

OP didn't ask for a list of the sums, so wouldn't [p for p in product(*l) if sum(p) == value] or something like that be better?
@ThisSuitIsBlackNot I'd probably do list(filter(lambda x: sum(x) == value, product(*l))) but you're right, I didn't read the requirements all the way
Out of curiosity, why would you prefer filter to a comprehension? Readability?
I did a lot of functional programming for a while, so that style is what i default to. Plus, setting up generator chains is very natural in python 3. It's more preference than anything else, and probably slower than the comprehension

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.