0

I have an if-statement that is shown below and I want to know, for the 4 variables that I am checking to see if the corresponding column in my file has the word 'TRUE' in it, how can I get python to output which exact variable that the if-statement is valid for?

For example: if the first row in my data set (which is a spreadsheet by the way) has the word 'true' in it for the variable A and 'false' for the rest, then my if-statement will be valid for that variable during that particular iteration of the loop. So, in that case, I want python to print out that it is variable "A", and so on for each iteration of the loop. How can I get the program to do that?

for x in range(1,len(column)):
    if A[x] or B[x] or C[x] or D[x] == "TRUE":
       # here I want to print out whichever variable the 'true' corresponds to
  • That code is **not** checking what you describe. Try to put `A[x] = True` before the loop... – Bakuriu Jan 10 '14 at 21:11
  • 4
    Aside: it seems like you think `A[x] or B[x] or C[x] or D[x] == "TRUE"` is the same as `(A[x] == "TRUE") or (B[x] == "TRUE") or (C[x] == "TRUE") or (D["x"] == "TRUE")`. That's not how it works, though. See [this question](http://stackoverflow.com/questions/15112125/if-x-or-y-or-z-blah). – DSM Jan 10 '14 at 21:12
  • What if multiple columns contain the word "TRUE"? Do you want to just return the first one that the code finds? – noblerare Jan 10 '14 at 21:14
  • Do you want to know which column has the test satisfying value or do you want to know the value of that column, i.e., A versus A[x]? –  Jan 10 '14 at 21:17

5 Answers5

4

You can do:

for x in range(1, len(column)):
    for name, var in [('A', A), ('B', B), ('C', C), ('D', D)]:
        if var[x] == "TRUE":
             print name
Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
2

First of all, your if statement does not do what you expect it to do. There are implicit braces which makes it look like this:

if (A[x]) or (B[x]) or (C[x]) or (D[x] == "TRUE"):

As you can see, the == "TRUE" part only applies to D[x] but not the other ones. Instead A[x], B[x] or C[x] having a “trueish” value will be enough to make the whole check evaluate to true. Even if they all contain "FALSE".

Instead, you need to repeat the == "TRUE" part for all:

if A[x] == "TRUE" or B[x] == "TRUE" or C[x] == "TRUE" or D[x] == "TRUE":

As you are checking for equality, you can also use the in operator to check if "TRUE" is in the list of all four columns:

if "TRUE" in (A[x], B[x], C[x], D[x]):

But as you want to find out, which of those four columns has the true value, you will need to split it up anyway:

if A[x] == "TRUE":
    # A is true
elif B[x] == "TRUE":
    # B is true
elif C[x] == "TRUE":
    # C is true
elif D[x] == "TRUE":
    # D is true
else:
    # neither is true
poke
  • 369,085
  • 72
  • 557
  • 602
  • Nice explanation of the problem (especially the other half of the problem, the half the OP didn't even know he had). – abarnert Jan 10 '14 at 21:21
0

It could be several, but it will only be checking until the first positive, thus even the interpreter will not know if C or D contain TRUE given B is TRUE. Split into multiple ifstatements on each column individually.

Oleg Sklyar
  • 9,834
  • 6
  • 39
  • 62
0

You don't actually need a separate statement for each one, you can just loop over them:

for thingy in A, B, C, D:
    if thingy[x] == "TRUE":
        print thingy

Or, if you want to do something more useful than just printing them out:

truethingies = [thingy for thingy in (A, B, C, D) if thingy[x] == "TRUE"]

If you just want the first one:

for thingy in A, B, C, D:
    if thingy[x] == "TRUE":
        print thingy
        break
    else:
        print "None of them. They're all lies."

… or…

truethingy = next(thingy for thingy in (A, B, C, D) if thingy[x] == "TRUE")
abarnert
  • 354,177
  • 51
  • 601
  • 671
0

It is likely, if I understand the problem description, that you would both want to know which main index values had any one of the indexed items satisfying the test as well as which one satisfied the test. I presume you want to test them in the specified order and short circuit like OR does.

anames = ['A', 'B', 'C', 'D']
arrays = [A, B, C, D]
for i in range(len(cols)):
   for j in range(len(arrays)):
      if arrays[j][i] == "TRUE":
        yield anames[j], i
        break