<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jul 14, 2014 at 5:10 PM, LJ <span dir="ltr"><<a href="mailto:luisjosenovoa@gmail.com" target="_blank">luisjosenovoa@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi All.<br>
<br>
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.<br>
</blockquote><div><br></div><div>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.</div><div><br></div><div>Something like (untested):</div>
<div>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.</div><div>
# Some code to add initial items.</div><div>while mySet:</div><div> item = mySet.pop()</div><div> # Do something with item, which may call mySet.add(), and possibly mySet.remove().</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
Thanks in advance!<br>
<span class=""><font color="#888888">--<br>
<a href="https://mail.python.org/mailman/listinfo/python-list" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br></div></div>