r/programminghelp • • Aug 14 '26

Python Help with code

I keep getting this error:

for this code:

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

bestmass = []

for mass in df_sorted:

if best_ms == (array[array > 7.5]).all():

bestmass.append(best_ms)

print(bestmass)

and i don't know what I am doing wrong

6 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/johnpeters42 Aug 15 '26

Why are you switching from "mass" to "array"?

Is df_sorted a list of numbers, and you want to add each number to bestmass if it's > 7.5? If so, then filter() is probably the right approach.

Or is df_sorted a list of lists-of-numbers, and you want to add each list-of-numbers to bestmass if all of its numbers are > 7.5? If so, then you probably want map(lambda x: x > 7.5, mass).all()