Gabriel Ryan Nahmias
Gabriel Ryan Nahmias

Reputation: 2159

Output Set and Its Contents in Python?

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!

Upvotes: 0

Views: 110

Answers (2)

Leonardo
Leonardo

Reputation: 1522

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 += "])"

Upvotes: 1

Peter DeGlopper
Peter DeGlopper

Reputation: 37344

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

Upvotes: 2

Related Questions