-1

what am I doing wrong here? Am trying to keep count of of when the value in a column/field of a row/array is less than all the other ones in the row.

So am trying to iterate through each row to check if a particular column is less than the rest of the column.

below code always results to one. So am super confused. Everything I try leads to 1.

def is_dog_correct(row, x, y, z, r):
    zero = 0
    for i in xrange(len(row)):
        if row[i-1][x] < row[i-1][y] and row[i-1][z] and row[i-1][r]:
            return Zero + 1
        else:
            return 0

Also, could there be an easier way of doing this. Am using graphlab.SFrame

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
Ikenna
  • 361
  • 1
  • 4
  • 6
  • 1
    Possible dupe: http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values – mgilson Nov 18 '16 at 18:12
  • 1
    Where is `Zero` defined? You have `zero` defined, but not `Zero`. – Makoto Nov 18 '16 at 18:12
  • 1
    `return` exits your function immediately, so the loop will only ever run through one iteration. – John Gordon Nov 18 '16 at 18:12
  • 1
    Also, due to the way that lists index, if you have a negative value as your index, you will get the last element. Because `i` starts at 0, `row[i-1]` will evaluate as `row[-1]`, which is the last element in that list. – Makoto Nov 18 '16 at 18:13
  • I suspect `if row[i-1][x] < row[i-1][y] and row[i-1][z] and row[i-1][r]` isn't what you mean to do. You're checking if `row[i-1][x] < row[i-1][y] ` is true and the value of `row[i-1][z]` is true and the value of `row[i-1][r]` is true. – Morgan Thrapp Nov 18 '16 at 18:15
  • Firstly you just shared the code and not the issue/error you are facing. Based on what I see, you initialized `zero = 0` but incrementing `Zero + 1` (with `z` capital) – Moinuddin Quadri Nov 18 '16 at 18:15
  • Also we do not know what values you are passing to `is_dog_correct(row, x, y, z, r)`. Without these basic information how do you think you would get the best help from SO community? Because we can not guess the actual problem you might be facing – Moinuddin Quadri Nov 18 '16 at 18:17
  • If the misnaming of `Zero` were really the issue, the code would throw a `NameError`, which it isn't... – John Gordon Nov 18 '16 at 18:17
  • This whole thing really needs a [mcve]. – Morgan Thrapp Nov 18 '16 at 18:18
  • 1
    @JohnGordon: As I mentioned, we do not have the complete information and this is just what I noticed. Maybe he has defined `Zero` somewhere in the outer scope and that is why not getting the exception. But it is for sure he needs `zero` instead of `Zero` – Moinuddin Quadri Nov 18 '16 at 18:19
  • Perhaps you want `if row[i-1][x] < min(row[i-1][y], row[i-1][z], row[i-1][r])`? you could write it more verbosely and pick up short-circuiting -- but it's pretty unlikely to make that much of a difference unless this is a really tight loop. – mgilson Nov 18 '16 at 18:21
  • Thank you guys. return is what was throwing me off. – Ikenna Nov 18 '16 at 19:40

1 Answers1

0

You need to keep counting and return the result after the loop is done.

def is_dog_correct(row, x, y, z, r):
    count = 0
    for i in xrange(len(row)):
        if row[i-1][x] < row[i-1][y] and row[i-1][z] and row[i-1][r]:
            count = count + 1
        else:
            pass
    return count
a.smiet
  • 1,727
  • 3
  • 21
  • 36
  • Why to even write the `else` part when it just only does `pass`? – Moinuddin Quadri Nov 18 '16 at 18:21
  • Of course it is not necessary, however, for a beginner I think it is more clear to leave it there. This is also why I didn't use count += 1 – a.smiet Nov 18 '16 at 18:28
  • No, it is not clear to use `pass`. And specially not to beginner. If you do, you are not a good mentor, because you taught him the wrong way to do thing at the first place. Using `count += 1` is also perfectly fine. If the OP do not know, he will get to know something new. Also, SO is not just for one user. Your answer will act as a reference for other users looking at this issue in the future. So, it should also take that as well into the consideration :) – Moinuddin Quadri Nov 18 '16 at 18:33
  • Thank you a.smiet. Figured out what the problem was prior to checking. I should have gotten an email, that the question has been answered to avoid 30 mins of brain freezes. So I could use count+=1 to replace the count = count +1? also I do not need the else as well? – Ikenna Nov 18 '16 at 19:39
  • Yes, `count += 1` is basically the short way for `count = count +1`. You don't need the else statement in this case, the `pass` statement just does nothing. @Moinuddin Quadri: If it is not clear to you to use `pass`, maybe it is clear for others, like in my case. People think in different ways and SO is not for mentoring someone but to answer specific questions. Moreover, to say it in your words: If the OP doesn't get the `pass` statement, he will get to know something new. – a.smiet Nov 18 '16 at 22:05