How to find numbers larger than X in a list

Asked

Viewed 846 times

1

As an example, this code there

N=[1,2,3,4,5]
B= #quantidade de números maiores que 2
print(B)

'B' would be the number of numbers greater than 2 present in the list, for example.

4 answers

4

The logic is exactly the same to filter elements from a list:

Filter elements from a Python list

The simplest and most direct is to use list comprehension:

B = len([i for i in N if i > 2])

In this case, we use the function len to calculate the amount of elements.

See working on Ideone.

Another way, equivalent to list comprehension, is to use the function filter:

B = filter(lambda i: i > 2, N)

However, the return of this function will be a generator, thus requiring the conversion to list to get its length:

B = len(list(filter(lambda i: i > 2, N)))

But for this solution, this method becomes unviable compared to the first.

See working on Ideone.

3


To simplify you can use like this:

minha_lista = [1,2,3,4,5]
maior_que = 2

filtrados = [x for x in minha_lista if x > maior_que]

#exibe os elementos
print(filtrados)

#conta os elementos
print(len(filtrados))
  • 1

    A lot more simplified than my answer, I’m still learning, and a lot.

  • @Wéllingthonm.Souza is one of the things I like about Python, I was reluctant to migrate to Python3.6 (I was "caught" in 2.7), but everything so far has pleased me, I’m happy to know that you are interested in language too :D

0

Here a solution, there can be easier and smaller

lista = [1,2,3,4,5,6,7,8,9,1,23]
X = 0
B = 0 # Armazena a quantidade de números maiores que 2
num_elementos_lista = len(lista)
while(X < num_elementos_lista):
    if lista[X] > 2: # verifica se lista[X] é maior que 2
      B+=1 # Se for incrementa + 1 em B
    X+=1

print(B)

See working on repl

  • I believe I have understood your idea and it is not all bad, but the implementation is wrong. Replace the list by [1, 1, 1, 1, 1] and see what happens. The expected result is 0, but... See if you understand the reason.

  • @Andersoncarloswoss actually I missed something at the time, I was checking to see if X was greater than 2, being that it was to verify lista[X] > 2

  • Perfect, now a suggestion that would make your solution more pythonica: to browse values from a list avoid using the while with the len, because it is a vice of other languages that is not only seen badly in the community as the performance is less. For this, prefer to use for n in lista, where in that case the n will be the values of the list, can do if n > 2: ..., no longer needing the len or the control variable X. See: https://ideone.com/8bghU2, which is nothing more than expanded list comprehension.

  • @Interesting andersoncarloswoss, vlw by tip.

0

If you intend apenas know the quantidade of values greater than a given value, which in this case is 2, you can implement a repeat loop and check the sum of all numbers larger than 2.

For this you can implement the following code...

N = [1, 2, 3, 4, 5]
cont = 0
for c in N:
    if c > 2:
        cont += 1

print(f'\033[32mA quantidade dos números maiores que "2" é: {cont}')

Note the functioning of the algorithm in repl it..

Note that in this algorithm I implemented a loop of repetition for, where he traverses the aforementioned list ([1, 2, 3, 4, 5]).

Then I defined a if where you will check each elemento from the list. If the widget is maior that 2 the counting variable shall be incrementada. And then displays the amount of values greater than 2.

Browser other questions tagged

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