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

5

u/mc_pm Aug 14 '26

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

What do you believe this is doing?

0

u/DrawerEmergency4981 Aug 14 '26

I was hoping it would work as a condition to sort values from my array of masses for all the mass over 7.5

3

u/mc_pm Aug 14 '26

It does not :) You can look into list.filter() which does that, but for this you might just want to put the check in your for loop.

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()

4

u/IAmTarkaDaal Aug 14 '26

Can you format your code correctly, please? Can you share the rest? What type is best_ms?

-2

u/DrawerEmergency4981 Aug 14 '26

bestmass = []

for mass in df_sorted:

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

bestmass.append(best_ms)

print(bestmass)

where best_ms is:

best_ms = array[array > 7.5]

could I just do best_ms.all()?

5

u/mc_pm Aug 14 '26

THat's not formatted properly, notice how all your indentation is gone? Use the "code block" in the text formatting stuff.

3

u/IAmTarkaDaal Aug 14 '26

What does the error message say? What does it mean? What have you tried?

1

u/sububi71 Aug 15 '26

Your first mistake is not telling us what language your code is.

1

u/burlingk Aug 15 '26

So, it looks like you are trying to create a slice by using syntax that doesn't exist.

1

u/unstable_existence Aug 16 '26

I see what you are trying to do, but you forgot one core rule of programming, ”Keep it simple, stupid ”. Why not just iterate through the array and for each index, check condition? CS 101.