Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'algorithm': 0.04; 'item,': 0.09; 'iterate': 0.09; 'subject:set': 0.09; 'cc:addr:python-list': 0.11; 'advance!': 0.16; 'elements,': 0.16; 'empty.': 0.16; 'iteration,': 0.16; 'pythonic': 0.16; 'set()': 0.16; 'set,': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'items.': 0.19; 'programming': 0.22; 'coding': 0.22; 'email addr:gmail.com>': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'initial': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'wonder': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'anyone': 0.31; 'this.': 0.32; 'handled': 0.32; 'regular': 0.32; 'url:python': 0.33; 'received:74.125.82': 0.34; 'knows': 0.35; 'problem.': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; '14,': 0.36; 'processed': 0.36; 'url:listinfo': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'should': 0.36; 'list.': 0.37; 'handle': 0.38; 'pm,': 0.38; 'received:74.125': 0.39; 'does': 0.39; 'flow': 0.39; 'structure': 0.39; 'url:mail': 0.40; 'removing': 0.60; 'solve': 0.60; 'to:addr:gmail.com': 0.65; 'jul': 0.74; 'typically,': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=txv7GHOvZ7ReFUB4b0ypoTcVg7GL3H5KXRyBfgE+2ug=; b=Y/NoaFMJ5E52UvmS7b3qAoHn00vu5TmmSIod1oGPI244nLfRrBfH4paW8frt0e2Mt2 t4YkedsDzWR28LBjoqMDYa6ieDJnsPfOjYmA4XB+x1eKBMkD6F0OU9t55P8gbWOCMSA1 7j+hu/BetgCgnpUTG2TeILSOoKLdgOq5BrkZaYQcAYV6v1V1SFD07ARMqFFzHVnLh9gt MdJhsYL2UorII8BPtlUMAL7+KpN3nWiwx+ZpYR2iO5N1g90WDYePy0P4BdBZtZMywzE5 yzxzNrQ5PuQk0WLXqZG55qfiY6CDhgrgm1dW8iacim9WcHwxq1MOFl3Zotfis+zWawVp cnyA== X-Gm-Message-State: ALoCoQk7I3WXgeLRXZTsjKpkTNLGtSoV8mbXGbk9MzBku0c09WvJ4CM2HjFc8UdBuly83DOIlTt0 X-Received: by 10.180.105.202 with SMTP id go10mr1555581wib.66.1405386543713; Mon, 14 Jul 2014 18:09:03 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Chris Kaynor Date: Mon, 14 Jul 2014 18:08:43 -0700 Subject: Re: Iterating through set To: LJ Content-Type: multipart/alternative; boundary=f46d0418271cb5ec1e04fe310da9 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405386551 news.xs4all.nl 2892 [2001:888:2000:d::a6]:60216 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74454 --f46d0418271cb5ec1e04fe310da9 Content-Type: text/plain; charset=UTF-8 On Mon, Jul 14, 2014 at 5:10 PM, LJ wrote: > Hi All. > > I'm coding a Dynamic Programming algorithm to solve a network flow > problem. At some point in the algorithm I have to iterate through a set of > nodes, while adding and/or removing elements, until the set is empty. I > know a regular set() object does not work in a case like this, so I wonder > if anyone knows of an efficient pythonic way to handle this. > Your description of your need is somewhat vague, but this sounds like a queue/stack which should be handled with a while loop and poping items. Something like (untested): mySet = [] # Typically, this would be a list. If you only want items processed once per iteration, you'd likely use a separate set, however the exact structure would vary based on the data and use-case. # Some code to add initial items. while mySet: item = mySet.pop() # Do something with item, which may call mySet.add(), and possibly mySet.remove(). > Thanks in advance! > -- > https://mail.python.org/mailman/listinfo/python-list > --f46d0418271cb5ec1e04fe310da9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On M= on, Jul 14, 2014 at 5:10 PM, LJ <luisjosenovoa@gmail.com> wrote:
Hi All.

I'm coding a Dynamic Programming algorithm to solve a network flow prob= lem. At some point in the algorithm I have to iterate through a set of node= s, while adding and/or removing elements, until the set is empty. I know a = regular set() object does not work in a case like this, so I wonder if anyo= ne knows of an efficient pythonic way to handle this.

Your description of your need is somewhat = vague, but this sounds like a queue/stack which should be handled with a wh= ile loop and poping items.

Something like (unteste= d):
mySet =3D [] # Typically, this would be a list. If you only want items= processed once per iteration, you'd likely use a separate set, however= the exact structure would vary based on the data and use-case.
# Some code to add initial items.
while mySet:
=C2=A0 = =C2=A0 item =3D mySet.pop()
=C2=A0 =C2=A0 # Do something with ite= m, which may call mySet.add(), and possibly mySet.remove().
=C2= =A0
Thanks in advance!
--
https://mail.python.org/mailman/listinfo/python-list

--f46d0418271cb5ec1e04fe310da9--