Python - en vez de append palabras, estoy append lineas

 
Vista:

en vez de append palabras, estoy append lineas

Publicado por Diego (1 intervención) el 03/11/2015 02:59:16
HOLA, TENGO EL PROBLEMA QUE ESTE PROGRAMA, EN VEZ DE HACER EL APPEND CON PALABRAS, LO HACE CON LINEAS.
A CONTINUACION, PONGO MI PROGRAMA, Y LUEGO DEL PROGRAMA, LA RESPUESTA DEL MENTOR A MI CONSULTA. PARECE QUE ES ALGO SIMPLE DE SOLUCIONAR, PERO YO NO LO VEO. AYUDA!!!

1
2
3
4
5
6
7
8
9
10
fname = raw_input("Enter file name: ")
fh = open(fname)
lista=[]
for elemento in fh:
    palabra=elemento.split()
    if palabra in lista:
        continue
    lista.append(palabra)
lista.sort()
print lista

Diego,
Is your output like this?
[ [some words] , [more words] ,[nearly done] , [last line] ]
That's a good output to have. Reading the file, getting each line to a list, appending lines to the output. Getting the lines of words as lists is a small change. Have a look at your variable names.
The program does this
file - line - words - check each word
instead of appending the word you have appended the line.
read a line get the list of words loop through the list of words check output list for duplicate word, save or skip? # after lines have been read sort print
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder