Created
June 27, 2012 17:22
-
-
Save codemaniac/3005522 to your computer and use it in GitHub Desktop.
Powerset in python using list comprehensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
def powerset(s): | |
return [[s[j] for j in xrange(len(s)) if (i&(1<<j))] for i in xrange(1<<len(s))] | |
s = ['a','b','c','d'] | |
print powerset(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment