0

I am working on a problem with project Euler and I need to get all combinations of adding int elements in a list,

from itertools import combinations
evenAbs = [12, 18, 20, 24, 30,36]
evenCombs =  sorted(([i+j for i,j in combinations(evenAbs, 2)]))

my problem is that I need the combinations to include 12+12 18+18 etc.. How can I do this?

1 Answer 1

2

Use itertools.combinations_with_replacement:

>>> import itertools
>>> list(itertools.combinations_with_replacement([1,2,3], 2))
[(1, 1), (1, 2), (1, 3), (2, 2), (2, 3), (3, 3)]
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a million, my answer was a a couple of hundred off the correct answer, having not realized until after many hours that combinations did not add values to themselves, the combinations_with_replacement did exactly what I needed!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.