itseva
itseva

Reputation: 593

Compute amount of numbers between 2 values in an array

I have already read several questions about this subject but I can not find a good answer to my specific question.

I have the list:

l = [0.25, 0.875, 0.7, 0.175, 0.6, 0.55, 0.525]

I have to write a method that returns a list of integers as result, namely the number of A scores, the number of B scores and the number of C scores, respectively. An A score is a score that is at least 60%, and a C score is a score that is strictly less than 50%. All other scores are B scores.

So, the list we need to create is:

[3, 2, 2]

I've tried working with this code:

        a = 0
        b = 0
        c = 0
        for i in l:
            if i>=0.6:
                a+=1
            elif i<0.5:
                c+=1
            else:
                b+=1
        return [a,b,c]

Does anyone know if there is a better solution to this problem?

Upvotes: 0

Views: 146

Answers (7)

Lambo
Lambo

Reputation: 109

Don't overcomplicate it. It is much better to write simple, understandable, and quick code than flashy code.

scores = [0.25, 0.875, 0.7, 0.175, 0.6, 0.55, 0.525]

a = b = c = 0

for score in scores:
    if score >= 0.6:
        a += 1
    elif score < 0.5:
        c += 1
    else:
        b += 1

tallies = [a, b, c]

The above code executes more quickly than other more complicated code.

You could also have the list to begin with:

abc = [0, 0, 0]
...
abc[0] += 1

Or a dictionary:

abc = {'a': 0, 'b': 0, 'c': 0}
...
abc['a'] += 1

Upvotes: 0

junglebambino
junglebambino

Reputation: 46

This code is working for me, I adapted your original idea.

[sum(1 for i in l if i >= 0.6) ,sum(1 for i in l if i < 0.6 and i>=0.5), sum(1 for i in l if i < 0.5 ) 

It gives you:

[3,2,2]

Upvotes: 1

Anton vBR
Anton vBR

Reputation: 18906

Inspired by Another SO-user, this I Think is the way to go:

from collections import Counter

l = [0.25, 0.875, 0.7, 0.175, 0.6, 0.55, 0.525]

def assign_grade(x):
    if x >= 0.6:
        return 'A'
    elif x < 0.50:
        return 'C'
    else:
        return 'B'

Counter([assign_grade(item) for item in l])

Returns a dict

Counter({'A': 3, 'B': 2, 'C': 2})

Upvotes: 0

hiro protagonist
hiro protagonist

Reputation: 46849

not very concise but pretty self-explaining using itertools.Counter:

from collections import Counter

l = [0.25, 0.875, 0.7, 0.175, 0.6, 0.55, 0.525]

def abc(score):
    if score < 0.5:
        return 'C'
    elif score < 0.6:
        return 'B'
    return 'A'

scores = Counter(abc(scr) for scr in l)
print(scores)  # Counter({'A': 3, 'B': 2, 'C': 2})

lst = [scr for abc, scr in sorted(scores.items())]
print(lst)  # [3, 2, 2]

this counts all the grades while iterating once over your list.

Upvotes: 1

vielkind
vielkind

Reputation: 2980

You can use list comprehension here to first map the scores to the grades. You can make a function to assign a score to each grade like this:

def assign_grade(x):
    if x >= 0.6:
        return 'A'
    elif x < 0.50:
        return 'C'
    else:
        return 'B'

where x is the score that will be input. Then you can call that function for each item in the list:

grades = [assign_grade(x) for x in scores]

Based on your example above grades would be a list of the grades for each score, ['C', 'A', 'A', 'C', 'A', 'B', 'B']

To get the count for each grade you can use the count function:

grade_sums = [grades.count(x) for x in ['A', 'B', 'C']]

which returns [3, 2, 2]

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71451

You can try this:

l = [0.25, 0.875, 0.7, 0.175, 0.6, 0.55, 0.525]

a_scores = [i for i in l if i >= 0.6]

b_scores = [i for i in l if i < 0.6 and i >= 0.5]

c_scores = [i for i in l if i < 0.5]

Output:

[0.875, 0.7, 0.6]

[0.55, 0.525]

[0.25, 0.175]

Upvotes: 0

Pruthvikar
Pruthvikar

Reputation: 439

sum() computes the sum of its arguments but i > 0.6 returns a bool

What you really want is to get the length of the sub array that passes a test

This is one way of getting the number of A-scores

len([i for i in l if i >= 0.6])

Upvotes: 1

Related Questions