【发布时间】:2021-02-19 05:25:26
【问题描述】:
我的代码必须把文件和所有单词放在一个列表中,然后检查它们是否不重复,如果需要删除它们直到它只有1。但是,只有“the”把它放了几次 在此处输入代码:
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
这是输出,为什么是 2 “the”?
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', '东方','羡慕','公平','悲伤','是','杀','光','月亮', '苍白','生病','柔软','阳光','the','the','through','what', 'window', 'with', '那边']
【问题讨论】:
标签: python python-3.x