【发布时间】:2018-04-05 19:37:30
【问题描述】:
我想取一段如下所示的文本:
Engineering will save the world from inefficiency. Inefficiency is a blight on the world and its humanity.
然后返回:
Engineering will save the world from inefficiency..is a blight on the . and its humanity.
也就是说,我想删除重复的单词并将它们替换为“。” 这就是我开始编写代码的方式:
lines= ["Engineering will save the world from inefficiency.",
"Inefficiency is a blight on the world and its humanity."]
def solve(lines):
clean_paragraph = []
for line in lines:
if line not in str(lines):
clean_paragraph.append(line)
print (clean_paragraph)
if word == word in line in clean_paragraph:
word = "."
return clean_paragraph
我的逻辑是创建一个包含所有最差字符串的列表,并将每一个添加到一个新列表中,然后,如果该单词已经在列表中,则将其替换为“.”。我的代码返回 []。任何建议将不胜感激!
【问题讨论】:
-
不应该返回。 “工程将拯救世界免于效率低下……是对……及其人性的一种破坏”。如果您要替换所有重复的单词
-
没错,除了
world这个词之外,the这个词也是重复的 -
是的,应该。我不确定我的代码哪里出错了
-
@user8827983 它应该返回一个字符串或一个列表?
标签: python string python-3.x duplicates words