| | |
Power Set List from Input List
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Returns the power set of the elements of a given list (even if some of those elements are also lists).
# From another DaniWeb snippet def int2bin(n, count=24): """returns the binary of integer n, using count number of digits""" return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)]) # PowerSet of a List def PowerSet(orignal_list): list_size = len(orignal_list) num_sets = 2**list_size powerset = [] # Don't include empty set for i in range(num_sets)[1:]: subset = [] binary_digits = list(int2bin(i,list_size)) list_indices = range(list_size) for (bit,index) in zip(binary_digits,list_indices): if bit == '1': subset.append(orignal_list[index]) powerset.append(subset) return powerset if __name__ == "__main__": print PowerSet([1,2,3])
Similar Threads
- Get Dropdown list value - set label text (ASP.NET)
- forcing set list in parameter list prompts? (C#)
- order results by set list (MySQL)
- List Input (Python)
- noob: list to set (Python)
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maintain maze module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlib



