Output Set and Its Contents in Python?

2019-09-02 04:45发布

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!

标签: python set
2条回答
倾城 Initia
2楼-- · 2019-09-02 04:48

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 += "])"
查看更多
爷、活的狠高调
3楼-- · 2019-09-02 05:06

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))
查看更多
登录 后发表回答