Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'distinct': 0.05; '"""': 0.07; 'below).': 0.07; 'constructor': 0.07; 'skip:` 10': 0.07; 'python': 0.08; 'box:': 0.09; 'context:': 0.09; 'corresponds': 0.09; 'empty,': 0.09; 'i.e.,': 0.09; 'invocation': 0.09; 'none:': 0.09; 'output': 0.10; 'def': 0.15; 'argument': 0.15; '(0,': 0.16; '(1,': 0.16; '(2,': 0.16; '1),': 0.16; '3),': 0.16; 'attributes.': 0.16; 'capacity.': 0.16; 'iterator': 0.16; 'iterators,': 0.16; 'length,': 0.16; 'message- id:@talk.nabble.com': 0.16; 'nabble.com.': 0.16; 'non-empty': 0.16; 'received:192.168.236': 0.16; 'received:192.168.236.156': 0.16; 'received:216.139': 0.16; 'received:216.139.236': 0.16; 'received:isper.nabble.com': 0.16; 'received:nabble.com': 0.16; 'url:nabble': 0.16; 'url:old': 0.16; 'advance': 0.18; 'subject:list': 0.18; 'specifies': 0.18; '(most': 0.21; 'trying': 0.21; "doesn't": 0.22; 'archive': 0.23; 'thus': 0.23; '(or': 0.23; 'assigning': 0.23; 'loop,': 0.23; 'produces': 0.23; 'in:': 0.24; 'object,': 0.24; 'skip:l 30': 0.24; 'traceback': 0.24; 'code': 0.25; 'work.': 0.27; 'classes': 0.28; 'raise': 0.28; '(see': 0.28; '(this': 0.28; 'import': 0.28; 'accommodate': 0.29; 'work:': 0.29; 'example': 0.30; 'ball': 0.30; 'completed.': 0.30; 'finite': 0.30; 'class': 0.30; 'moving': 0.31; 'skip:b 30': 0.31; 'specified': 0.31; 'skip:- 70': 0.31; 'list': 0.32; 'break': 0.32; 'does': 0.32; 'initial': 0.32; "what's": 0.33; 'to:addr:python-list': 0.33; 'calling': 0.33; 'right,': 0.34; 'integer': 0.34; 'apply': 0.35; 'supposed': 0.35; 'object': 0.35; 'unless': 0.36; 'charset :us-ascii': 0.36; 'problems': 0.36; 'none': 0.37; 'sequence': 0.37; 'skip:z 20': 0.37; 'but': 0.37; 'subject:can': 0.38; 'overview': 0.38; 'some': 0.38; 'received:192': 0.39; 'suggestions': 0.39; 'appreciated.': 0.39; 'define': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'mailing': 0.39; 'called': 0.40; 'subject:from': 0.40; 'where': 0.40; 'total': 0.61; 'maximum': 0.63; 'box,': 0.64; 'home': 0.66; 'limit': 0.66; 'view': 0.67; 'unlimited': 0.68; 'protect': 0.76; 'boxes.': 0.84; 'move.': 0.84; 'numbered': 0.84; 'self.n': 0.84; 'limits:': 0.91 Date: Fri, 9 Sep 2011 16:45:44 -0700 (PDT) From: "Dr. Phillip M. Feldman" To: python-list@python.org Subject: can't generate iterator from list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nabble-From: Phillip.M.Feldman@gmail.com 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: 180 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1315612886 news.xs4all.nl 2524 [2001:888:2000:d::a6]:33459 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:13039 It is supposed to be possible to generate a list representation of any iterator that produces a sequence of finite length, but this doesn't always work. Here's a case where it does work: Input: from itertools import combinations list(combinations(range(4),2)) Output: [(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)] When I define my own classes that produce iterators, conversion to a list works for some of these classes but not for others. Here's a case where it doesn't work: In: list(balls_in_numbered_boxes(2, [3,3,3])) Out: [array([0, 0, 1]), array([0, 0, 1]), array([0, 0, 1]), array([0, 0, 1]), array([0, 0, 1])] Note that if I apply the `next` method to the object, the output is correct: In [5]: x=balls_in_numbered_boxes(3,[3,3,3]) In [6]: x.next() Out[6]: array([3, 0, 0]) In [7]: x.next() Out[7]: array([2, 1, 0]) In [8]: x.next() Out[8]: array([1, 2, 0]) In [9]: x.next() Out[9]: array([0, 3, 0]) In [10]: x.next() Out[10]: array([0, 2, 1]) In [11]: x.next() Out[11]: array([0, 1, 2]) In [12]: x.next() Out[12]: array([0, 0, 3]) In [13]: x.next() --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) Code is attached (see below). Any suggestions as to what's going wrong will be appreciated. class balls_in_numbered_boxes(object): """ OVERVIEW This class generates an iterator that produces all distinct distributions of indistinguishable balls among numbered boxes with specified capacity limits. (This is a generalization of the most common formulation of the problem, where each box is sufficiently large to accommodate all of the balls, and is an important example of a class of combinatorics problems called 'weak composition' problems). CONSTRUCTOR INPUTS n: the number of balls limits: This argument is a list of length 1 or greater. The length of the list corresponds to the number of boxes. `limits[i]` is a positive integer that specifies the maximum capacity of the ith box. If `limits[i]` equals `n` (or greater), then the ith box can accommodate all `n` balls and thus effectively has unlimited capacity. """ def __init__(self, n=None, limits=None): if n < 0 or not isinstance(n,int): raise BadInput("The number of balls n must be a non-negative integer.") if not isinstance(limits,list) or len(limits)<1: raise BadInput("`limits` must be a non-empty list.") for limit in limits: if not isinstance(limit,int) or limit<1: raise BadInput("Items in `limits` must be positive integers.") # Copy constructor inputs to object attributes. We make a `deepcopy` of # `limits` to protect against the possibility of the calling program # modifying it before all calls to the `next` method have been completed. self.n= n self.limits= deepcopy(limits) self.distribution= None def __iter__(self): return self def next(self): # If `self.distribution` is `None`, this is the initial call to `next`, # in which case we generate the initial distribution by assigning as many # balls as possible to the first box, as many balls that remain to the # next box, and so on. if self.distribution is None: self.distribution= zeros(len(self.limits), dtype='i4') balls= self.n for box in xrange(len(self.limits)): # Store as many balls as possible in the current box: self.distribution[box]= min(balls,self.limits[box]) balls-= self.distribution[box] if balls == 0: break else: # We fell through the above loop, which means that it was impossible # to distribute all of the balls: raise BadInput("The total capacity of the boxes is less than the " "number of balls to be distributed.") # Make first box the "current" box, i.e., the box from which a ball # will be moved when the `next` method is invoked: self.box= 0 return self.distribution # `self.distribution` is not `None`, which means that this is not the # initial invocation of `next`. We create the next distribution by moving # one ball to the right, unless this is impossible. self.distribution[self.box]-= 1 for box in xrange(self.box+1,len(self.limits)): # If this box is full, advance to the next one: if self.distribution[box] == self.limits[box]: continue self.distribution[box]+= 1 break else: # We fell through the above loop, which means that it was impossible # to find a new home for the ball that we were trying to move. raise StopIteration # If the current box--the box from which we have been removing balls-- is # empty, advance to the next box: if self.distribution[self.box] == 0: self.box+= 1 return self.distribution -- View this message in context: http://old.nabble.com/can%27t-generate-iterator-from-list-tp32435519p32435519.html Sent from the Python - python-list mailing list archive at Nabble.com.