Reputation: 101
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?
Upvotes: 1
Views: 7353
Reputation: 300
The error means that an you cannot call len
on numpy.float64
object so you need to do this different way.
Assuming that chi_sq_values
is plain list, here's how to do it fast
np.count_nonzero(np.asarray(chi_sq_values) > 1.6)
Upvotes: 0
Reputation: 1425
In your first example, you iterate the items of the list and access directly the value. However, the item doesn't have `len()'. You need to generate the list on your own, in order to evaluate the length.
chi_sq_values = [1, 2, 3]
greater = []
for value in chi_sq_values:
if value > 1.6:
print(value)
greater.append(value)
print(len(greater))
Upvotes: 3
Reputation:
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)
Upvotes: 3