Replacing values in a list

1

I need to replace values in a list of agreements with some criteria.

Criteria:

  • If the value is an integer and less than the value entered, replace -1
  • if the value is a floating-point number and greater than the number you enter replace with 9999.0.
  • Code:

    lista =[-9 ,20.5, 6 ,10.8, 10, 8.0, 45, -99.6, 12, -54.7]
    valores = input('Digite um valor inteiro e um valor em ponto   flutuante:').split()
    num1 = int((valores[0]))
    num2 = float((valores[1]))
    for i in range(len(lista)):
        if int(num1):
            if lista[i] < num1:
                lista[i]= -1
        if float(num2):
            if lista[i] > num2:
                lista[i] =9999.0
    
    print(lista)
    

    Correct output would be:

    -1 9999.0 -1 9999.0 10 8.0 45 -99.6 12 -54.7
    

    The output of my code is:

    -1, 9999.0, -1, 9999.0, 9999.0, -1, 9999.0, -1, 9999.0, -1
    

    Friends could solve it as follows.

    Code:

     lista =[-9 ,20.5, 6 ,10.8, 10, 8.0, 45, -99.6, 12, -54.7]
     valores = input('Digite um valor inteiro e um valor em ponto   flutuante:').split()
     num1 = int((valores[0]))
     num2 = float((valores[1]))
     for i in range(len(lista)):
        if type(lista[i]) is int and lista[i] < num1:
           lista[i] = -1
        elif type(lista[i]) is float and lista[i] > num2:
           lista[i] = 9999.0
     print(lista)
    
        
    asked by anonymous 23.04.2018 / 22:19

    1 answer

    1

    A little more simplified than your solution

    lista =[-9 ,20.5, 6 ,10.8, 10, 8.0, 45, -99.6, 12, -54.7]
    valor_inteiro, valor_float = [float(x) for x in input('Digite um valor 
    inteiro e um valor em ponto flutuante:').split()]
    
    for i in range(len(lista)):
        if isinstance(lista[i], int) and lista[i] < valor_inteiro:
            lista[i] = -1
        if isinstance(lista[i], float) and lista[i] > valor_float:
            lista[i] = 9999.0
    
    print(lista)
    
        
    25.04.2018 / 15:30