Iterating through set
ckaynor at zindagigames.com
Tue Jul 15 03:08:43 CEST 2014
On Mon, Jul 14, 2014 at 5:10 PM, LJ <luisjosenovoa at 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.
item = mySet.pop()
# Do something with item, which may call mySet.add(), and possibly
> Thanks in advance!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list