0

Newbie here. Writing function that prints out all the subsets of a set using generator (yield statement). The problem is, I cannot make it so that it uses yield and works for sets of all lenghts, not only three, like in my code below. I can make it work for 4 and 5 obviously, but the ladder gets bigger, which is just ridiculous, let alone pythonic and elegant.

ABC = ['a', 'b', 'c']

def subsets(group):
    for i in [group[0], []]:
        for j in [group[1], []]:
            for k in [group[2], []]:
                yield filter(None, [i, j, k])

for element in subsets(ABC):
    print element

The output (for this 3-item list) should be:

['a', 'b', 'c']
['a', 'b']
['a', 'c']
['a']
['b', 'c']
['b']
['c']
[]
Victor
  • 31
  • 7
  • Briefly: loop over `range(1, len(ABC)+1)` and use `itertools.combinations`. – TigerhawkT3 Jan 25 '16 at 23:30
  • Thanks, but the point is to make it using a generator, I'm learning this stuff. – Victor Jan 25 '16 at 23:36
  • `def subsets(group): for counter in range(1, len(group)+1): for subset in itertools.combinations(group, counter): yield subset`? That's just [this particular answer](http://stackoverflow.com/a/5898031/2617068)'s method, with `yield` instead of `print`, making a nice little generator. – TigerhawkT3 Jan 25 '16 at 23:42
  • The logic behind the itertools module is hidden from me. I need to write this code w/o imports, this is the point of the lesson. I can't nail it and wanted good people to help me see what i'm doing wrong making that horrible ladder, which I can't avoid. I tried to change indeces in "[group[0], []]" with a variable, but then I don't what to yield (see my code). I'm stuck on that =\ – Victor Jan 26 '16 at 01:01
  • Then that would be [this question instead](http://stackoverflow.com/questions/20764926/combinations-without-using-itertools-combinations). – TigerhawkT3 Jan 26 '16 at 01:06
  • That guy needed "to create combinations for two elements a time", which is not the same as the listing of all the subsets. I've added the desired output to make it clear. – Victor Jan 26 '16 at 01:27
  • Did you look at the top answer on that question? It contains a reproduction of the pure Python implementation of `itertools.combinations`, from [the documentation](https://docs.python.org/2/library/itertools.html#itertools.combinations). – TigerhawkT3 Jan 26 '16 at 01:49
  • these are `combinations`, I need `subsets`. That code output: `# combinations('ABCD', 2) --> AB AC AD BC BD CD` The output I need is in my question, It's different. – Victor Jan 26 '16 at 02:05
  • Why do you keep ignoring the part about `for counter in range(1, len(group)+1):`? And why would copy-pasting the pure Python implementation of `itertools.combinations` be a more effective lesson than copy-pasting an `itertools.combinations` call? Read the linked duplicate questions to learn about `itertools.combinations` and how it can be used to produce the output you want, read the question I linked above to see a pure Python implementation of `itertools.combinations`, and then put them together, replacing the `itertools.combinations` call with the pure Python implementation. – TigerhawkT3 Jan 26 '16 at 02:21

0 Answers0