2

in the following code yield command is used to generate returned values. I'd like to avoid using it, replacing it with iteration operations. How can I do it?

def triangulations(p):
    n = len(p)
    if n == 2:
         yield []
    elif n == 3:
        yield [p]
    else:
        for k in range(1, n - 1):
            for u, v in product(triangulations(p[:k + 1]), triangulations(p[k:])):
                yield u + [(p[0], p[k], p[-1])] + v

print(list(triangulations(tuple("abcde"))))
RamsesXVII
  • 295
  • 2
  • 11
  • It depends on how this generator (triangulations) is called elsewhere in the code. Can you post that part of the code? – perigon Jul 19 '17 at 10:04
  • print(list(triangulations(tuple("abcde")))) – RamsesXVII Jul 19 '17 at 10:05
  • First, you should probably take a look at this brilliant anwser to make sure you understand generators: https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python – maestromusica Jul 19 '17 at 10:07
  • what do you mean by "iteration operations"? – Alex Hall Jul 19 '17 at 10:08
  • 1
    @maestromusica Yes, in particular, [this](https://stackoverflow.com/a/45129224/4909087) answer. :p – cs95 Jul 19 '17 at 10:08
  • 4
    Would like to share *WHY* you would like to avoid `yield`? Maybe the underlying reason is something to be discussed because trying to avoid it seems very questionable to me. It's such a great feature that I would rather understand if someone would like to use it instead of avoid it. – Alfe Jul 19 '17 at 10:11

1 Answers1

4
def triangulations(p):
    result = []
    n = len(p)
    if n == 2:
         result.append([])
    elif n == 3:
         result.append([p])
    else:
        for k in range(1, n - 1):
            for u, v in product(triangulations(p[:k + 1]), triangulations(p[k:])):
                result.append( u + [(p[0], p[k], p[-1])] + v)
    return result

for i in triangulations([1, 2, 3]):
    print(i)

More explained:

def triangulations(p):
    result = []
    n = len(p)
    print ("execute triangulations")
    if n == 2:
         result.append([])
    elif n == 3:
         result.append([p])
    else:
        for k in range(1, n - 1):
            for u, v in product(triangulations(p[:k + 1]), triangulations(p[k:])):
                result.append( u + [(p[0], p[k], p[-1])] + v)
    return result



triangulationsResult = triangulations([1, 2, 3])
for i in triangulationsResult:
    print(i)
for i in triangulationsResult:
    print(i)

def triangulationsYield(p):
    n = len(p)
    print ("execute triangulationsYield")
    if n == 2:
         yield []
    elif n == 3:
        yield [p]
    else:
        for k in range(1, n - 1):
            for u, v in product(triangulations(p[:k + 1]), triangulations(p[k:])):
                yield u + [(p[0], p[k], p[-1])] + v

triangulationsResultYield = triangulationsYield(["a", "b", "c"])
for i in triangulationsResultYield:
    print(i)
for i in triangulationsResultYield:
    print(i)

output:

execute triangulations
[[1, 2, 3]]
[[1, 2, 3]]
execute triangulationsYield
[['a', 'b', 'c']]
1408786user
  • 1,868
  • 1
  • 21
  • 39
  • https://stackoverflow.com/a/231855/1408786 See this answer for more information, this should explain everything about the yield function. – 1408786user Jul 19 '17 at 10:14
  • 2
    I know what `yield` (and as far as I know, it's not a function). Basically, a code dump is not a good answer, so you should explain how and why this code works. – Right leg Jul 19 '17 at 10:16
  • It simply replaces all occurrences of `yield` by adding the value to a result list and in the very end returns this result list. – Alfe Jul 19 '17 at 10:20
  • I have updated my answer, is it now clear enough for you? Basically without yield it keeps the result in memory. With the yield every time you iterate it needs to execute the function (more cpu intensive if you need to iterate multiple times, but consuming less memory) – 1408786user Jul 19 '17 at 11:10