José_Ricardo
José_Ricardo

Reputation: 11

why python is printing 2 "the"s, is an error?

my code has to take the file and put all the words in a list, and then check if they are not repeated, if necessary delete them until it is only 1. However, only the "the" puts it several times enter code here:

    plbrs_totales = list()
    fnombre = input("Ingrese nombre de archivo:\n")
    try:
      llave = open(fnombre)
    except:
      print("Error")
      quit()
    for lineas in llave:
      lineas_ind= lineas.rstrip()
      plbrs = lineas_ind.split()
      plbrs_totales = plbrs_totales + plbrs
    for rep in plbrs_totales:
      repwords = plbrs_totales.count(rep)
      if repwords > 1:
       plbrs_totales.remove(rep)
    plbrs_totales.sort()
    print(plbrs_totales)

y este es el archivo.txt:

But soft what light through yonder window breaks\n
It is the east and Juliet is the sun\n
Arise fair sun and kill the envious moon\n
Who is already sick and pale with grief\n

and this is the output, why are 2 "the"?

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']

Upvotes: 1

Views: 51

Answers (1)

Nerveless_child
Nerveless_child

Reputation: 1410

That's because you are modifying the list you are iterating over. This should answer the why.

As to how you could do it, you would want to replace the second for loop with this.

plbrs_totales = list(set(plbrs_totales))
plbrs_totales.sort()
print(plbrs_totales)

The set method will return a set with each item in plbrs_totales occuring once while the list method converts the set back to a list.

Upvotes: 0

Related Questions