code for computing and printing list of combinations
Tim Chase
python.list at tim.thechases.com
Tue Mar 20 11:33:10 EDT 2012
On 03/20/12 09:59, Joi Mond wrote:
> To All, Can someone help me with the proper code to compute
> combinations for n=7, r=5 for the following list of numbers:
> 7, 8, 10, 29, 41, 48, 55. There should be 21 combination. Also
> once there list is made can a code be written to add (sum)
> each of the set of five number in the the list. For example
> list: 8, 10, 29, 48, 55, = 150. Thanks
Sounds like you want to read up on itertools.combinations() and
the sum() function.
http://docs.python.org/library/itertools.html#itertools.combinations
or
>>> import itertools
>>> help(itertools.combinations)
This sounds like a homework problem, so I won't hand you the
answer, but you pass your list to combinations() with the
grouping-size (r). You then iterate over the results of that,
and use sum() on the results.
-tkc
More information about the Python-list
mailing list