我只是想输出 a 的内容set
(因为我必须使用这种被诅咒且不灵活的类型),但每次我去做或循环并打印每个元素时,我都会得到最后一个而不是其余的。这是我的整个代码:
def factorial(n):
r = 1
for i in range(1, n + 1):
r *= i
return r
def nCr(n, r):
return factorial(n) / (factorial(r) * factorial(n - r))
def combinations(s, tokens):
maxcombos = int(nCr(len(s), tokens))
for index in range(maxcombos):
token_copy = tokens
combo = []
for val in range(1, len(s) + 1):
if not token_copy:
break
thresh = nCr(len(s) - val, token_copy - 1)
if index < thresh:
combo.append(s.pop())
token_copy -= 1
else:
index -= thresh
yield tuple(combo)
def powerset(s):
pset = set()
for num_tokens in range(1, len(s)):
for combo in combinations(s, num_tokens):
pset.add(combo)
pset.update(s)
#pset.update(tuple())
return pset
if __name__ == '__main__':
s = set([1,2,23])
p = powerset(s)
msg = "Initial set: set(["
for (i, item) in enumerate(s):
msg += str(item) + ", "
msg += "])"
print(msg)
#print(powerset())
我只是希望每个人都([])
像正常的集合一样出现在它们之间。有没有办法简单地输出整个变量及其内容?谢谢!