Reputation: 41
I have a user to input list values which can be anything like 1 0.95 0.9 0.7 0.6 The numbers will be in decreasing order always and the numbers below 0.6 will always be considered as less than 0.6. Now I have a list of values ranging from 0-1 some 35 values and I need to divide the values in these categories.
I am having no idea on how to proceed with it, since the user can give any number of values from 0.6-1.0
I tried finding how many values are there greater than 0.6 but then don't know what to do further in order to divide the list of different values in the corresponding ranges.
This is the code I have tried to identify how many values are greater than 0.6, is there 0.6 and how many values are less than 0.6, given by the user.
greater = 0
equal = 0
lower = 0
for i in args['range']:
if i > 0.6:
greater += 1
elif i == 0.6:
equal += 1
else:
lower += 1
suppose there is a list of numbers n = [0.0, 0.2, 0.4, 0.2, 0.8, 0.7, 0.1, 0.3, 0.5]
and the user has given values like: 1 0.99 0.8 0.6 0.4
Now, the program should distribute the values such as
values_equal_to_1 = 0
values_between_0.99_and_0.8 = 1
values_between_0.6_and_0.8 = 1
values_less_than_0.6 = 7
Upvotes: 0
Views: 312
Reputation: 29742
Using numpy.searchsorted
and collections.Counter
:
import numpy as np
from collections import Counter
user_input = '1 0.99 0.8 0.6 0.4'
intv = sorted([float(i) for i in user_input.split() if float(i) >=0.6])
# [0.6, 0.8, 0.99, 1.0]
n = [0.0, 0.2, 0.4, 0.2, 0.8, 0.7, 0.1, 0.3, 0.5]
d = Counter(np.searchsorted(sorted(intv), n, 'right'))
Output of d
:
Counter({0: 7, 1: 1, 2: 1})
You can then make some representation such as :
{'values less than or equal to %s' % i: d.get(n,0) for n, i in
enumerate(sorted(intv))}
Output:
{'values less than or equal to 0.6': 7,
'values less than or equal to 0.8': 1,
'values less than or equal to 0.99': 1,
'values less than or equal to 1.0': 0}
Upvotes: 0
Reputation: 9575
Just use list comprehensions:
values_equal_to_1 = len([x for x in list if x == 1])
values_between_99_and_8 = len([x for x in list if .8 < x < .99])
values_between_6_and_8 = len([x for x in list if .6 < x < .8])
values_less_than_6 = len([x for x in list if x < .6])
list
is the list of values the user enters. Don't actually name it list
as that will override built-ins
Upvotes: 0