Select integer floats in a list,

0

From a file with numbers in the list I must select the floating ones of the integers, for that I must transform them from strings to floating and integers, having to differentiate which ones are floating and which integers, for this I have designed the following program, but the failure it is in which the floats of more than one decimal do not identify them, although he said that for this he must identify the character "."

nombre_entrada = raw_input('Nombre del fichero de entrada: ')
f_entrada = open(nombre_entrada, 'r')
lista = []
for i in f_entrada:
    if i[-1] == '\n':
        lista.append(i[:-1])
    else:
        lista.append(i)

lista2 = []
for i in lista:
    print i
    for caracter in i:
        if caracter == ".":
            lista2.append(float(i))
            lista.remove(i)

print lista
print lista2
    
asked by FJCorro 01.01.2019 в 22:44
source

3 answers

0

Solved:

nombre_entrada = raw_input('Nombre del fichero de entrada: ')
f_entrada = open(nombre_entrada, 'r')

# Lo primero que tenía que hacer era extraer los números aun como strings 
# del archivo sin el retorno de carro '/n' y agregarlos a una lista.
lista = []
for i in f_entrada:
    if i[-1] == '\n':
        lista.append(i[:-1])
    else:
        lista.append(i)

#Después convertirlos todos a flotantes para poder realizar una división.       
lista2 = []
for i in lista:
    lista2.append(float(i))

#Una vez tenido los flotantes puedo realizar una división, si tiene resto
#es flotante, de lo contrario es entero. 
#(la forma de mostrarlo en pantalla es irrelevante, solamente es petición 
#del ejercicio)

for i in lista2:
    if i%1 == 0:
        print '%05i' % i
    elif i%1 <> 0:
        print '%5.2f' % i

f_entrada.close()
    
answered by 02.01.2019 / 16:28
source
0

Welcome FJCorro, I do not think it is necessary to explain all the complete code to give you an answer. Going to the reasoning that you have, the floating numbers have a point that separates the integers from the decimals, and that's fine, but you're not working clearly with numbers. The best way to do it would be:

lista = [ 1.01, 2, 3.2, 4, 5.5, 3, 3, 3, 3, 3.2]

lista2 = []
for i in lista:
    if i%1 != 0 :
        lista2.append(float(i))
        lista.remove(i)
print lista
print lista2

I made a modification to your code so that you can directly evaluate the values already defined in the array list [] Only with a conditional knowing that the rest of any division of a whole number between 1 will always be equal to 0.

The result of this code is:

>>> 
[2, 4, 3, 3, 3, 3]
[1.01, 3.2, 5.5, 3.2]
>>> 
    
answered by 02.01.2019 в 01:30
0

There is something useful to know if there is one string inside another. It works in the following way, if you have a string 'Hola Mundo' and you want to know if the string 'h' are in the string, you simply use 'h' in 'Hola Mundo' and this will return a boolean that will say whether or not it is in the string (in this case False ). Now in your separate code you should close the file once it is finished, so you should put f_entrada.close() at the end of your code. Then your code would be:

nombre_entrada = raw_input('Nombre del fichero de entrada: ')
f_entrada = open(nombre_entrada, 'r')
floats = []
enteros = []
for i in f_entrada:
    # Si tiene un punto, es un float
    if '.' in i:
        floats.append(float(i))
    # Si no, es un entero
    else:
        enteros.append(int(i))

# Para cerrar el archivo
f_entrada.close()

print floats
print enteros

Aside I think I've noticed that you've done some things so that your code ignores the \n (it's a space for the next line) or something ... Quiet when you do f_entrada = open(nombre_entrada, 'r') , you have a list in your code call f_entrada that has each line of the file nombre_entrada as a string.

    
answered by 06.01.2019 в 21:30