Last active
October 5, 2016 03:55
-
-
Save brycepg/f8366d86637a3ac2727d26d1a18736b2 to your computer and use it in GitHub Desktop.
Generate List Combinations Using Bitwise Arithmetic
This file contains 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
def combinate(list_): | |
"""Generate all combinations of a list(unordered)""" | |
combinations = [] | |
for count in range(1, 2**len(list_)): | |
combinations.append( | |
[elem for cur_index, elem in enumerate(list_) | |
if (count >> cur_index) & 1] | |
) | |
return combinations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment