Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21934 > unrolled thread
| Started by | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| First post | 2012-03-20 10:33 -0500 |
| Last post | 2012-03-20 10:33 -0500 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: code for computing and printing list of combinations Tim Chase <python.list@tim.thechases.com> - 2012-03-20 10:33 -0500
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-03-20 10:33 -0500 |
| Subject | Re: code for computing and printing list of combinations |
| Message-ID | <mailman.833.1332257573.3037.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web