Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21934

Re: code for computing and printing list of combinations

Date 2012-03-20 10:33 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: code for computing and printing list of combinations
References <1332255545.34960.YahooMailClassic@web126001.mail.ne1.yahoo.com>
Newsgroups comp.lang.python
Message-ID <mailman.833.1332257573.3037.python-list@python.org> (permalink)

Show all headers | View raw


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


Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: code for computing  and printing list of combinations Tim Chase <python.list@tim.thechases.com> - 2012-03-20 10:33 -0500

csiph-web