Powersets of a list?
David C. Ullrich
ullrich at math.okstate.edu
Sun May 27 14:45:05 EDT 2001
On Sun, 27 May 2001 10:28:57 -0700, Corran Webster
<cwebster at nevada.edu> wrote:
>In article <f9712f33.0105251051.4163a0ee at posting.google.com>, Walter
>Vannini <walterv at jps.net> wrote:
>
[...]
>
>Refining this a bit and putting it into one function gives:
>
>def PowerSet(list):
> PowerSetSoFar = [[]]
> for element in list:
> PowerSetSoFar += [i+[element] for i in PowerSetSoFar ]
> return PowerSetSoFar
>
>which may be the best solution offered so far.
>
>For MacPython 2.1 on a 400 Mhz B&W G3 running OS X, this is comparable
>to or slightly better than the recursive algorithm given by David
>Urlich.
[timings of various power sets snipped]
Thanks (I _hate_ to install new software, even
new versions of Python - now I can put off 2.x
a little longer...)
Two questions: Why did you omit the bitshifting ones
from the range(18) test, and who's this David Urlich
guy? His code looks vaguely familiar but I can't place
the name.
Yes, it does look like list comprehensions are nice.
David C. Ullrich
***********************
"Sometimes you can have access violations all the
time and the program still works." (Michael Caracena,
comp.lang.pascal.delphi.misc 5/1/01)
More information about the Python-list
mailing list