Output Set and Its Contents in Python?

无人久伴 提交于 2019-12-13 04:37:21

问题


I am merely wanting to output the contents of a set (because I have to use this cursed and inflexible type), but every time I go to do it or loop through and print each element, I get the last one instead of the rest. Here's my entire code:

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())

I merely want each one to appear in between those ([]) like a normal set does. Is there a way to simply output the whole variable with its contents? Thanks!


回答1:


Your powerset function actually alters its input. In combinations, you use s.pop(). That's almost certainly wrong, and would explain why its contents have changed when you try to print it.

The simplest fix is to replace your powerset function with the recipe from the itertools documentation: http://docs.python.org/2/library/itertools.html#recipes

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



回答2:


Change the top of your combinations definition to the following:

def combinations(r, tokens):
    s = set(r)
    # everything else you had

This creates a new set which will not be altered in the combinations function. The reason it was being altered before is because the local variable in combinations was a reference to the same object outside the function. If two names reference the same object, mutating one will affect the other.

In main you want the following:

for (i, item) in enumerate(s):
    msg += str(item) + ", "
msg = msg[:-2]
msg += "])"


来源:https://stackoverflow.com/questions/19308021/output-set-and-its-contents-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!