Number of numbers greater than 7 in a list in Python

3

I want to know how many numbers are greater than 7 in the 'a' list.

  

Note: I'm using slicing because in the case I'm putting in   practice, I do not know how many items you have on the list.

a=[5, 10, 6, 8]
qmaior= a.count([:-1])>7

When executed, the syntax error.

    
asked by anonymous 05.11.2017 / 04:21

3 answers

2

So I understand you want to figure out which numbers are bigger than 7 in the A list, so you need to filter to get numbers greater than 7. I'm still crawling in Python, that's how I got there:

(I noticed that I had asked to count how many are greater than 7, my read error, count added, if you do not want to appear which ones are larger, delete the print ))

a=[5, 10, 6, 8]

qmaior = 7

filtro_lista = [c for c in a if c > qmaior]

print(filtro_lista)

print('Quantidade de números maior que 7:', len(filtro_lista))
    
05.11.2017 / 05:42
1

One more option:

def maiorq(num=None, lista=None):
    return sum( n > num for n in lista)

if __name__ == '__main__':
    valor = maiorq(num=7, lista=[10,8,2,4,60,3])
    print(valor)
    
05.11.2017 / 08:13
0

You can use the sum() function combined with a Expressão Geradora to get the quantity of items in a list that have a value greater than 7 :

a = [5, 10, 6, 8]
print sum( i > 7 for i in a )

Output:

2
    
05.11.2017 / 14:51