Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:code': 0.07; 'compute': 0.09; 'iterate': 0.09; '-tkc': 0.16; '55,': 0.16; 'answer,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'message-id:@tim.thechases.com': 0.16; 'received:50': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject:combinations': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'written': 0.19; 'cc:no real name:2**0': 0.21; 'subject:list': 0.21; 'header:In-Reply-To:1': 0.22; 'cc:2**0': 0.26; 'code': 0.26; 'all,': 0.27; 'import': 0.27; 'pass': 0.29; 'example': 0.29; 'cc:addr:python.org': 0.29; 'url:library': 0.31; 'thanks': 0.32; 'that,': 0.32; 'list': 0.32; 'there': 0.33; "won't": 0.33; 'header:User-Agent:1': 0.33; 'someone': 0.34; 'function.': 0.34; 'list.': 0.35; 'list:': 0.35; 'url:python': 0.35; 'but': 0.37; 'should': 0.38; 'url:org': 0.39; 'your': 0.61; 'results': 0.64; '29,': 0.73; 'subject: ': 0.74; 'to:addr:yahoo.com': 0.83; 'subject:printing': 0.84; 'numbers:': 0.91 Date: Tue, 20 Mar 2012 10:33:10 -0500 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Joi Mond Subject: Re: code for computing and printing list of combinations References: <1332255545.34960.YahooMailClassic@web126001.mail.ne1.yahoo.com> In-Reply-To: <1332255545.34960.YahooMailClassic@web126001.mail.ne1.yahoo.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332257573 news.xs4all.nl 6938 [2001:888:2000:d::a6]:56436 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21934 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