Compute amount of numbers between 2 values in an array

时间:2017-08-04 13:06:54

标签: python arrays python-3.x list

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?

7 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

使用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]

这会计算所有成绩,同时在列表上进行一次迭代。

答案 2 :(得分:1)

这段代码对我有用,我改编了你原来的想法。

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

它给你:

[3,2,2]

答案 3 :(得分:0)

你可以试试这个:

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]

输出:

[0.875, 0.7, 0.6]

[0.55, 0.525]

[0.25, 0.175]

答案 4 :(得分:0)

您可以在此处使用列表推导来首先将分数映射到成绩。您可以创建一个功能,为每个年级分配一个分数,如下所示:

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

其中x是将要输入的分数。然后,您可以为列表中的每个项目调用该函数:

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

根据上面的例子,成绩将是每个分数的分数列表,['C','A','A','C','A','B','B'] < / p>

要获得每个年级的计数,您可以使用计数功能:

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

返回[3,2,2]

答案 5 :(得分:0)

不要过分复杂化。编写比华而不实的代码更简单,易懂和快速的代码要好得多。

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]

上面的代码比其他更复杂的代码执行得更快。

您也可以将列表开头:

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

或字典:

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

答案 6 :(得分:0)

受到另一位SO用户的启发,我认为这是要走的路:

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

返回一个字典

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