0

I'm trying to compare 3 different arrays to create an output where if the value of the three arrays is below 30, then the output is 1, if one of above 30, then the output to be 0. I want to create a resulting array of 1's and 0's, however I keep getting the error -

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

Am I just way off on my coding? I've only been coding for 2 days so I don't have a large idea about what I'm doing.

#loop through each row of the array
for row in UrbanArray:
    #loop through each value in the current row
    for v in row.flat:
        if (UrbanArray < 30 and AgArray < 30 and NatArray < 30):
            tmpList.append(1)
        else:
            tmpList.append(0)
kuro
  • 3,214
  • 3
  • 15
  • 31
  • 1
    Why are you comparing `UrbanArray`, `AgArray`, and `NatArray` to 30? – user2357112 Jun 12 '17 at 17:16
  • 1
    You're trying to compare your whole array to 30. Instead, you want to compare a single element in your array to 30, or as the Python interpreter suggests, compare ```any``` element in your array to 30 (true if any element is less than 30) or ```all``` elements in your array to 30 (true if all elements are less than 30). Although my use of the word "array" here is a misnomer in Python. A ```list``` is probably the closest thing to a C array. – mgarey Jun 12 '17 at 17:17
  • What is AgArray and NatArray where they come from? – vZ10 Jun 12 '17 at 17:19
  • 1
    @mgarey I would say an `array.array` is the closest thing to a C array. And clearly, the OP is using `numpy`, because this is a very common error. And `numpy` arrays are basically C arrays, and not like lists. – juanpa.arrivillaga Jun 12 '17 at 17:23
  • Thanks! I think it worked? it started showing a different syntax error further in my script so I dont know if its coming out as what I want but I will find out. Unless i get stuck again... – Kimberly Broadbelt Jun 12 '17 at 17:25
  • @juanpa.arrivillaga Yeah, you're right. – mgarey Jun 12 '17 at 17:27
  • @KimberlyBroadbelt What exactly are you trying to accomplish here? I think you might just want `(UrbanArray < 30) & (AgArray < 30) & (NatArray < 30)`, which will return a boolean array, no loops necessary. – juanpa.arrivillaga Jun 12 '17 at 18:04
  • related?: https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous – David Cary Sep 15 '20 at 20:41

0 Answers0