Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74454

Re: Iterating through set

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 <ckaynor@zindagigames.com>
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&gt;': 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 <d70b2c77-d3d5-42f8-9c8e-d25ef78b3963@googlegroups.com>
References <d70b2c77-d3d5-42f8-9c8e-d25ef78b3963@googlegroups.com>
From Chris Kaynor <ckaynor@zindagigames.com>
Date Mon, 14 Jul 2014 18:08:43 -0700
Subject Re: Iterating through set
To LJ <luisjosenovoa@gmail.com>
Content-Type multipart/alternative; boundary=f46d0418271cb5ec1e04fe310da9
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11817.1405386551.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Mon, 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
> 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
>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Iterating through set LJ <luisjosenovoa@gmail.com> - 2014-07-14 17:10 -0700
  Re: Iterating through set Roy Smith <roy@panix.com> - 2014-07-14 20:30 -0400
  Re: Iterating through set Chris Kaynor <ckaynor@zindagigames.com> - 2014-07-14 18:08 -0700
  Re: Iterating through set Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2014-07-17 15:22 +0200

csiph-web