Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'skip:[ 20': 0.04; 'subject:Python': 0.06; '3),': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:How': 0.10; '(0,': 0.16; '0),': 0.16; '1),': 0.16; '2),': 0.16; '4),': 0.16; 'appreciated!': 0.16; 'code?': 0.16; 'feasible': 0.16; 'hint': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:under': 0.16; 'subject:values': 0.16; 'x_1': 0.16; 'x_2': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'subject:all': 0.32; 'etc': 0.35; 'should': 0.36; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'first': 0.61; 'subject:find': 0.84; 'zhang': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Python: How to find out values of all feasible x under constraints. Date: Fri, 15 May 2015 09:47:28 +0200 Organization: None References: <12da2c70-fd4e-430b-83d2-fad4ac1e3f8f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8d50.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431676060 news.xs4all.nl 2826 [2001:888:2000:d::a6]:50619 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90655 Xiang Zhang wrote: > I want to know how to find out values of all feasible x under constraints. > > x = [x_1, x_2, x_3,..., x_10] > > > constraints: > x_i = 0,1,2,3 or 4, where i=1,2,....10 > x_1 + x_2 + x_3 +...+x_10 <= 15 That are 5**10 == 9765625 candidates. That's still feasible to check using brute force. > How to find out all the feasible solutions x (domain of x) using python, > like [0,0,0,0,0,0,0,0,0,0], [1,1,1,1,1,1,1,1,1,1] etc ? What should be the > code? > > Any hint or help would be highly appreciated! >>> solutions = [x for x in itertools.product(range(5), repeat=10) if sum(x) <= 15] >>> len(solutions) 1556215 The first ten solutions: >>> solutions[:10] [(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0, 1), (0, 0, 0, 0, 0, 0, 0, 0, 0, 2), (0, 0, 0, 0, 0, 0, 0, 0, 0, 3), (0, 0, 0, 0, 0, 0, 0, 0, 0, 4), (0, 0, 0, 0, 0, 0, 0, 0, 1, 0), (0, 0, 0, 0, 0, 0, 0, 0, 1, 1), (0, 0, 0, 0, 0, 0, 0, 0, 1, 2), (0, 0, 0, 0, 0, 0, 0, 0, 1, 3), (0, 0, 0, 0, 0, 0, 0, 0, 1, 4)]