Dear Readers

I wonder if there is an equivalent in Stata to "itertools" in python? For a large number of cases I have a list of numbers of different length and I want to find all the combinations of those numbers that add to a given number. In Python this can be done for example, with the list of numbers [0.02, 0.01, 0.18, 0.04, 0.03] to add to 0.25) :

. python
----------------------------------------------- python (type end to exit) ---------------------------------------------------------------------------------------------------------------------------------------
>>> import itertools
... numbers = [0.02, 0.01, 0.18, 0.04, 0.03]
>>>
>>> result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == 0.25]
>>> print result
[(0.02, 0.01, 0.18, 0.04), (0.18, 0.04, 0.03)]
>>> end

As a way round this I have written a python script which I call from Stata

Script:

#python script combinations.py
import sys
import itertools
print sys.argv
arguments = len(sys.argv) - 1
arguments
position = 1
while (arguments >= position):
print ("parameter %i: %s" % (position, sys.argv[position]))
position = position + 1
numbers = str(sys.argv[1])
print numbers
# seems to be list of strings
length = len(numbers)
length
goal = float(sys.argv[2])
print goal
result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == goal]
print result

Call from Stata

local numbers `"[0.02, 0.01, 0.18, 0.04, 0.03]"'
local target = 0.25
python script [path]/combinations.py, args(`"`numbers'"' "`target'")

which gives:

. python script d:/india/banerjee&iyer/gis/dams/nrld/DoFiles/python/combinations.py, args(`"`numbers'"' "`target'")
['d:/india/banerjee&iyer/gis/dams/nrld/DoFiles/python/combinations.py', '[0.02, 0.01, 0.18, 0.04, 0.03]', '.25']
parameter 1: [0.02, 0.01, 0.18, 0.04, 0.03]
parameter 2: .25
[0.02, 0.01, 0.18, 0.04, 0.03]
0.25
Traceback (most recent call last):
File "d:/india/banerjee&iyer/gis/dams/nrld/DoFiles/python/combinations.py", line 24, in <module>
result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == goal]
TypeError: unsupported operand type(s) for +: 'int' and 'str'
failed to execute the specified Python script file
r(7103);

end of do-file

But running the following irectly in Stat gives the correct results

************************************************** ***********
local numbers `"[`0.02', `0.01', `0.18', `0.04', `0.03']"'
local target = 0.25
python
import sys
import itertools
numbers = `numbers'
print numbers
target = float(`target')
print target
result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == target]
print result
end
************************************************** **************

. local numbers `"[0.02, 0.01, 0.18, 0.04, 0.03]"'

. local target = 0.25

. python
----------------------------------------------- python (type end to exit) ---------------------------------------------------------------------------------------------------------------------------------------
>>> import sys
>>> import itertools
>>> numbers = `numbers'
>>> print numbers
[0.02, 0.01, 0.18, 0.04, 0.03]
>>> target = float(`target')
>>> print target
0.25
>>> result = [seq for i in range(len(numbers), 0, -1) for seq in itertools.combinations(numbers, i) if sum(seq) == target]
>>> print result
[(0.02, 0.01, 0.18, 0.04), (0.18, 0.04, 0.03)]
>>> end

It is seemingly the way the argument "numbers" is treated as all string in the args() option to the "python script" call, but I cannot work out how to get around it.

Thanks for help

Richard