Take values less than or equal to 500 from the list - Python

Asked

Viewed 193 times

1

Code:

acid_vit_lista = [100,500, 200, 230, 400, 500, 600, 700, 900, 2000, 1100]
acid_vit_dig = int(input('\nNumero de acidentes de transito: '))

res_acid = (acid_vit_lista[num_dig])# num_dig foi da lista anterior
soma_acid = res_acid + acid_vit_dig
print(soma_acid)

quant_vit = len(acid_vit_lista)
maior_acid = max(acid_vit_lista)
menor_acid = min(acid_vit_lista)
print(f'O maior índice de acidentes foi {maior_acid}, e o menor índice foi {menor_acid}')

I need to get all the numbers menores ou iguais 500 and average. How can I do that?

I’m at the beginning of the programming.

Thanks in advance for your attention!

  • 1

    Get values less than or equal to 500: lista_filtrada = filter(lambda x: x <= 500, acid_vit_lista), average: media = sum(lista_filtrada) / len(lista_filtrada)

3 answers

5

If there is a need to generate a new list (and given the semantics of the problem I think valid), it is best to use list comprehension (or comprehensilist on).

numbers = [100,500, 200, 230, 400, 500, 600, 700, 900, 2000, 1100]
numbers_ge_500 = [number for number in numbers if number >= 500]

So basically you create a new list only with the values of numbers that are greater than or equal to 500. To calculate the average it is sufficient to calculate the sum by dividing by the number of numbers:

media = sum(numbers_ge_500) / len(numbers_ge_500)

1


To do this you have to go through the values of your list and if it is less than or equal to 500 add to a second list.

Then, to make the average just tame all the values of the second list and divide by the number of elements. More or less like this:

acid_vit_lista = [100,500, 200, 230, 400, 500, 600, 700, 900, 2000, 1100]
segunda_lista = []
soma = 0

for x in acid_vit_lista:
    if x <= 500:
        segunda_lista.append(x)

for x in segunda_lista:
    soma += x
    
print(soma/len(segunda_lista))

Of course you can do all of this through the original list, without the need to create the second, but from what I understand from your question, a second list is the wish, and maybe it’s easier since it’s starting.

Similarly the sum of the elements for the calculation of the average could also be done within the first for, but made it separate to facilitate understanding.

I believe that optimizing the code with the tips I gave can be a good exercise for you to study.

P.S. I removed the accent from your list name. Never use accents for variables.

  • A way inline to do this is with the function mean module statistics thus: statistics.mean(filter(lambda x: x <= 500, acid_vit_lista)).

  • Thanks Evilmaax! Helped a lot! Valew!

  • 1

    Blz! I did what you asked, but you say you don’t appear in public. But it was worth it!

1

From what I understand, you want to implement an algorithm that takes all the values menores ou iguais 500 from a given list and calculate your Arithmetic Mean.

Well, you no need to create another list. Just scroll through the original list through a repeat loop for and check that each item of the respective interaction is menor ou igual to 500. If so, we count the times such values appear in the list - variable cont - and accumulate the values of such variables in the variable soma. Then we calculate the quotient between soma and cont.

That way, the algorithm would be:

acid_vit_lista = [100, 500, 200, 230, 400, 500, 600, 700, 900, 2000, 1100]

cont = soma = 0
for item in acid_vit_lista:
    if item <= 500:
        cont += 1
        soma += item

media = (soma / cont)
print(f'\033[32mA media aritmética é: {media:.2f}')

Note that this algorithm goes through the list acid_vit_lista and checks that each item in the list is menor ou igual to 500. Positive case, accumulates the amount of occurrences in the variable cont and accumulates the value of each occurrence in the variable soma.

It then calculates the média aritmética of the respective values and then displays the value with duas decimal places.

Observing

The arithmetic mean between N values is the ratio of the sum of the values to the number of values, that is, the variable "soma" divided by the variable "cont".

Now if you want to generalize this algorithm to calculate the arithmetic mean of all values menores ou iguais to 500 of a any list, you can use the following algorithm:

acid_vit_lista = list(map(int, input('Digite o número de acidentes de trânsito: ').split()))

cont = soma = 0
for item in acid_vit_lista:
    if item <= 500:
        cont += 1
        soma += item

media = (soma / cont)
print(f'\033[32mA media aritmética é: {media:.2f}')

When we run the second algorithm we receive the following message: Digite o número de acidentes de trânsito: . Right now we must type all the values, in the same line, separated for a single space and press enter. From that moment the algorithm will calculate the arithmetic mean of the values menores ou iguais to 500.

What is the difference between the first and second algorithm?

The first algorithm is capable of working with only one list, that is, the list that was previously written in the algorithm. The second algorithm, on the other hand, is capable of working with an indefinite number of lists. In this second algorithm, at each execution of the same, we can enter different values to assemble the said list.

  • Very good explanation. I’m starting on this. Thank you!

Browser other questions tagged

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