我正在执行一个程序,使用true
函数删除Python中列表中的重复单词。
我的回答是错误的,因为它没有删除重复的元素。
romeo.txt:
split()
我的代码:
But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief
答案 0 :(得分:1)
append()
将字词添加到列表中。示例:
line="But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief"
arr=list()
count=0
words=line.split()
for word in words:
if word not in arr:
arr.append(word)
arr.sort()
print(arr)
输出
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']