Skip to content
Advertisement

object of type ‘numpy.float64’ has no len(): How can I fix this?

I’m trying to calculate the total number of values above 1.6 in my list of 10,000 numbers. I’ve tried a few ways:

for value in chi_sq_values:
    if value > 1.6:
        print(len(value))

gives me the error in my title but the following works

greater = [i for i in chi_sq_values if i > 1.6]
total = len(greater)
print(total)

I want to try two methods to see validate my answer, how can i fix the first one so it works?

Advertisement

Answer

In the first code snippet, you try printing the len of value. As numpy.float64 has no len, you get an error.

Therefore, in order to count all values bigger then 1.6, you can simply use a counter:

counter = 0
for value in chi_sq_values:
    if value > 1.6:
        counter += 1
print(counter)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement