I need to do this in Python.
There is a given list l,may contain more than 5000 integer elements.
There is a limit on sum of the numbers,20000 or may be high.
The output should be all the possible sums of 2 numbers picked from list,
Like,
l=[1,2,3,4,5,6,7,8,9]
output
1+1,1+2,1+3,1+4,1+5,1+6...........
2+2,2+3,2+4.......
.........
.......
2,3,4,5,6... like that
I’m using this code,Doing this for now,
But It’s slow
l=listgen()
p=[]
for i in range(0,len(l)):
for j in range(i,len(l)):
k=l[i]+l[j]
if k not in p:
p.append(k)
p.sort
print(p)
listgen() is the function that generating the input list.
Some old-fashioned optimization might get you faster code that’s easier to grok than list comprehensions with multiple for loops:
as an added bonus, this version will optimize beautifully with psyco, cython, etc.
Update:
When comparing this to the other suggestions (replacing listgen with range(5000), I get: