Number greater than 7 in a Python list

Asked

Viewed 942 times

3

I want to know how many numbers is more than 7 on the 'a list'.

Note: I’m using slicing because in the case I’m putting in practice, do not know the amount of items you have in the list.

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

When executed, syntax error.

3 answers

2

From what I understand you want to find out which numbers are bigger than 7 on list A, so you need to filter to get numbers bigger than 7. I’m still crawling in Python, this is the way I got:

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

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

1

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

1

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

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

Exit:

2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.