python-自定义电源设置功能

时间:2018-06-21 05:58:55

标签: python powerset

我正在尝试在python中执行此代码,并且编译时没有错误。但是,我在变量浏览器中看不到变量z。我正在尝试创建一个提供输入集所有子集的函数。

import numpy as np
import itertools as itt

def powerset(iterable):
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return itt.chain.from_iterable(itt.combinations(s, r) for r in 
range(len(s)+1))

def powerset_generator(i):
for subset in itt.chain.from_iterable(itt.combinations(i, r) for r in 
range(len(i)+1)):
yield set(subset)

z=powerset({1,2,3})

1 个答案:

答案 0 :(得分:0)

简单的Powerset功能

import math 

def powerSet(LIST,SIZE): 
    sizeOfAll = (int) (math.pow(2, SIZE))
    i = 0
    store = 0
    print("Φ")
    for i in range(1, sizeOfAll):
        store = format(i,'0'+str(SIZE)+'b')
        j=0
        for j in range(0,SIZE):
            if store[j] == '1':
                print(LIST[j],end="")
        print()

powerSet(['a', 'b', 'c','d'], 4)