0

I have a collection of data on which i would like to apply while loop but i got an error

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

import numpy as np


data = np.loadtxt('data4.txt')
i=np.array(data[:,1])    #column having thousand values 

j=7/2

f = i-j

while f <= i+j:
    print(f)
    f = f+1
  • 2
    Have you tried searching for that error on Google? It's not clear what you're trying to do but this is equivalent to saying `if 10 <= [2, 4, 6, 8, 10, 12]`. How is that to be interpreted? – roganjosh Jul 04 '18 at 16:20
  • Can you clarify at which line the error is thrown? – kosnik Jul 04 '18 at 16:20
  • at this line "while f <= i+j:" – Hamza Hanif Jul 04 '18 at 16:43
  • @roganjosh: I am trying to range of "i-j, (i-j)+1,(i-j)+2 .... ,i+j" where j is constant while 'i' have different values – Hamza Hanif Jul 04 '18 at 16:46
  • How do you end with `i+j` in that? Maybe `(i-j)+2j`? But anyway, you're still applying that to an array, so your intent is not clear to me. Should it stop when one value in that array crosses the threshold, or all of them? We can't see your input data. – roganjosh Jul 04 '18 at 16:48
  • @roganjosh Related to physics, The possible values of F levels are | I - J |, | I - J | + 1, . . . . . . I + J. – Hamza Hanif Jul 04 '18 at 16:55
  • 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

2 Answers2

1

Have you tried to use this? The solution might be in the output actually. Hope this works for you

import numpy as np  
data = np.loadtxt('data4.txt')
i=np.array(data[:,1])    #column having thousand values 

j=7/2   
f = i-j
while np.all(f <= i+j):
    print(f)
    f += 1
roganjosh
  • 12,594
  • 4
  • 29
  • 46
Nicola
  • 446
  • 7
  • 17
  • 2
    Nitpick but `f += 1` would probably be faster because it can work in-place on arrays. And something tells me that this could be solved much easier without the `while` loop at all, but that's another thing because the OP hasn't explained what they're trying to do :) – roganjosh Jul 04 '18 at 16:38
  • Addressed your input. I also agree there should be a quicker way but the output message for once is pretty leading – Nicola Jul 04 '18 at 16:41
  • Oh yeah, I'm not disputing the fact that the error is clear and your answer addresses the issue sorry. – roganjosh Jul 04 '18 at 16:42
0

You may have some data in the f array evaluate to True for being less than or equal to the Cartesian Sum of i and j, while some data in the f array evaluate to False.

So which one do you pick for truthiness? That's where any() and all() come into play:


Any

condition = i + j

while not (f - condition).any():  # If any elements of f are not greater than i + j
    ...
    condition = i + j

All

condition = i + j

while not (f - condition).all():  # If not all of the elements of f are greater than i + j
    ...
    condition = i + j
the_constant
  • 681
  • 4
  • 11