Hi All<br><br>With your help I found lots of good method and algorithm. Also I found out if I exchange all for loop with while loop it make the program much faster and also it consumes less memory (almost half!)<br>Just wanna thank you all.
<br>Cheers,<br>Arash<br><br><div><span class="gmail_quote">On 7/13/07, <b class="gmail_sendername">Mark Dickinson</b> <<a href="mailto:dickinsm@gmail.com">dickinsm@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
If you don't care about the order of the results, you can use a Gray<br>code (<a href="http://en.wikipedia.org/wiki/Gray_code">http://en.wikipedia.org/wiki/Gray_code</a>): this has the advantage<br>of only adding or removing a single element to get from one subset to
<br>the next.<br><br>def powerset(s):<br>    d = dict(zip(<br>            (1<<i for i in range(len(s))),<br>            (set([e]) for e in s)<br>            ))<br><br>    subset = set()<br>    yield subset<br>    for i in range(1, 1<<len(s)):
<br>        subset = subset ^ d[i & -i]<br>        yield subset<br><br>>>> list(powerset('abc'))<br>[set([]), set(['a']), set(['a', 'b']), set(['b']), set(['c', 'b']),
<br>set(['a', 'c', 'b']), set(['a', 'c']), set(['c'])]<br><br>If you're using the subsets as they appear and don't need to store<br>them all at once, then it's significantly faster (on my machine) if
<br>you replace the line subset = subset ^ d[i & -i] with an in-place<br>update:  subset ^= d[i & -i].<br><br>Mark<br><br><br>--<br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list
</a><br></blockquote></div><br>