0

I have the following problem in which I want to generate patterns with a 0 introduced. The problem is that this should be space efficient, and thus I would like to use a generator that creates and returns one of the patterns at a time (i.e. I do not want to create a list and return it.) I am at a loss here because I don't know what I am doing wrong in calling the generator, so any help would be appreciated!

original pattern = [ 0. 1. 1. 2.] generator should generate and return these: [ 0. 0. 1. 2.], [0. 1. 0. 2.], [ 0. 1. 1. 0.]

However I can just yield one of the patterns with a call to the generator (the additional 1.0 is not an issue just to not cause any confusion...) and not all of them in succession.

yielded subpattern = (array([ 0., 1., 0., 2.]), 1.0)

The following is my code:

import numpy as np
def removeoneletter(subpattern):
     # generator = []
     originalpattern = subpattern
     subpatterncopy = subpattern.copy()
     nonzero = np.nonzero(subpattern)
     listnonzero = nonzero[0].tolist()
# only replace elements at nonzero positions with zero
     for i in listnonzero:
          subpatterncopy = subpattern.copy()
          position = i
          transaction = subpatterncopy[i]
          np.put(subpatterncopy, position, 0)
          print('subpattern generator')
          print(subpatterncopy)
          # generator.append((subpatterncopy, transaction))
          yield((subpatterncopy, transaction))
          if i == len(listnonzero):
              break

and the function gets called by another function like so:

def someotherfunction():

  combinations = removeoneletter(subpatterncopy)

  for x in combinations:
       # x = next(combinations)
       print('yielded subpattern')
       print(x)
  • sure sorry I didn't see it was not maintained. I added spaces now and hope they are now accurate. Thank you. – trummelbummel Oct 18 '16 at 08:29
  • You should mention that this is a numpy problem and add "import numpy as np" accordingly. – Lucas Kahlert Oct 18 '16 at 09:06
  • hey yes I have added the import. However it isn't really a numpy problem. Because the code generates the patterns just fine. It is a problem with yielding from a generator. However I realized that it does what it should do with the code posted. But something in the back of someotherfunction did mess up what I was trying to do. It was basically an indentation issue. I have solved it now. Maybe the rest of the code can help another person. So I will rename the question. – trummelbummel Oct 18 '16 at 10:04

1 Answers1

0

Not being an numpy expert, this part looks a bit weird to me:

for i in listnonzero:
    ....
    if i == len(listnonzero):
          break

Your for loop assigns elements of listnonzero to i, and then you test that against the length of the list. If the listnonzero had five elements [5,42,6,77,12] and the first element you pick from the list happens to be number 5, you would break the loop and exit after the first iteration.

Your code would (probably) work in Javascript or similar where "for i in something" assigns an index to i instead of a list member.

If this does not help, try removing yield and adding a print statement there to see if your listnonzero and i are what you expect them to be.

Just a thought.

Hannu

Hannu
  • 11,685
  • 4
  • 35
  • 51