2

Is there an alternative (even if longer) method of writing the yield part of this code? I'm not very familiar with the function and would rather keep it simple.

for i in range(0, len(lstInput) - intCount + 1):
    if intCount == 1:
        yield [lstInput[i]]
    else:
        current = lstInput[i]
        remainder = lstInput[i+1:]
        for rest in yieldLotto(remainder, intCount - 1):
            yield [current] + rest
user3290553
  • 87
  • 1
  • 2
  • 9
  • u can use a list and append to that – sundar nataraj May 27 '14 at 04:45
  • 8
    [How about instead you learn what `yield` does?](http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained) – jwodder May 27 '14 at 04:47
  • 1
    Seriously, though, `yield` is not complex. Just take the 20 minutes to learn it completely and you'll never worry about it again. @jwodder's SO link is perfect and even has some links to slideshows if I remember correctly. (It's how I learned yield). – woot May 27 '14 at 04:52

1 Answers1

3

The alternative is to either embed the loops into your calling program, or to change it to return a list. This means everything will be going into memory, though.

def foo():
    return_list = []
    for i in range(0, len(lstInput) - intCount + 1):
        if intCount == 1:
            return_list.append([lstInput[i]])
        else:
            current = lstInput[i]
            remainder = lstInput[i+1:]
            for rest in yieldLotto(remainder, intCount - 1):
                return_list.append([current] + rest)
    return return_list

Honestly though, I think yield is better and it's an important feature of Python. I suggest learning it.

woot
  • 7,406
  • 2
  • 36
  • 55