How to show a list of values of a register applying filters on numeric values?

Asked

Viewed 52 times

1

I have to show all register with numeric field filter, I have to choose which numeric field, set lower and upper limit ex.: Show all parts with price >= to 45.50 and price <= to 90.00

I have a code-compliant list:

cadastroPeca = [] 
item = []

def mostrarCadastro():
  print("Cadastro atualizado:\n")
  print(cadastroPeca)
  print("")

def mostrarCadastro2(lista, filtro1, filtro2):
  for i in range(len(lista)):
    if i == filtro1 and i == filtro2:
      if i >= filtro1 or i <= filtro2:
        print(lista)
      else:
        print("Não possue!")

print("\nFiltro de Pesquisa")
    print("1- Código")
    print("2- Preço")
    print("3- Quantidade")
    print("")

    mostrarCadastro()

    campoEscolhido = input("Escolha o campo que dejesa pesquisar digitando pelo número: ")

    if campoEscolhido != "1" and campoEscolhido != "2" and campoEscolhido != "3":
      print("\nOpção digitada não existe na lista acima! Por favor, escolha uma das opções abaixo.\n")

    campoEscolhido1 = int(campoEscolhido)

    if campoEscolhido2 == 1:
      print("Opção selecionada: ", campoEscolhido2)
      filtroMenor = int(input("Informe o menor valor do item de pesquisa: "))
      filtroMaior = int(input("Informe o maior valor do item de pesquisa: "))

I wanted to know what might have been missing in my function to return the values between two variables.

2 answers

0

In case I have a list list as excerpt:

cadastral = [] item = []

fieldChooded = input("Choose the field you want to search by typing the number: ")

  if campoEscolhido != "1" and campoEscolhido != "2" and campoEscolhido != "3":
    print("\nOpção digitada não existe na lista acima! Por favor, escolha uma das opções abaixo.\n")

  else:
    campoEscolhido1 = int(campoEscolhido)

    if campoEscolhido1 == 1:
      print("Opção selecionada: ", campoEscolhido1)
      filtroMenor = int(input("Informe o menor valor do item de pesquisa: "))
      filtroMaior = int(input("Informe o maior valor do item de pesquisa: "))


mostrarCadastro2(cadastroPeca, filtroMenor, filtroMaior)

This introducing me this error: Traceback (Most recent call last): File "python", line 120, in File "python", line 27, in show Typeerror: '>=' not supported between instances of 'list' and 'int'

0


1) When you compare the i, you are comparing the index of the list, not the values. To compare the values, use lista[i];

2) Its function has a print(lista) within the for, i.e., for each iteration of the for, it will print something;

3) if i == filtro1 and i == filtro2: that line made no sense;

4) if i >= filtro1 or i <= filtro2: here is not or, is a and, besides being lista[i], and not i;

5) I recommend using list comprehension.

Setting up your function:

def mostrarCadastro2(lista, filtro1, filtro2):
    lista_filtrada = []
    for i in range(len(lista)):
        if lista[i] >= filtro1 and lista[i] <= filtro2:
            lista_filtrada.append(lista[i])
    return lista_filtrada #ou print(lista_filtrada)

Other ways of doing:

def mostrarCadastro2(lista, filtro1, filtro2):
    return list(filter(lambda x: x >= filtro1 and x <= filtro2 , lista))

#Ou ainda

def mostrarCadastro2(lista, filtro1, filtro2):
    return [ num for num in lista if (num >= filtro1 and num <= filtro2) ]

Browser other questions tagged

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