«

【Python程序设计】移除重复文本

azurekiln 发布于 阅读:2 Python程序设计


编写程序检查文本中重复的单词,并只保留一个,例如,文本内容为“This is is a desk.",输出为 "This is a desk."

测试文本为:

'''Once upon a a time, there was was a man who wanted to steal his neighbor’s doorbell.

However, he knew clearly that the bell would ring and catch the other people’s attention

as as long as he touched the bell.

So so he thought hard and suddenly hit on a clever “idea”. '''

text = '''Once upon a a time, there was was a man who wanted to steal his neighbor’s doorbell. 

However, he knew clearly that the bell would ring and catch the other people’s attention 

as as long as he touched the bell.

So so he thought hard and suddenly hit on a clever “idea”. '''

real_content = []
# 根据句号拆分成每一句
for sentence in text.split("."):
    real_sentence = []
    # 根据逗号拆分成每一部分
    for part in sentence.split(","):
        real_part = []
        check_part = []
        # 再拆分成每个单词
        for word in part.split():
            word_2 = word.lower()
            if word_2 not in check_part:
                real_part.append(word)
                check_part.append(word_2)
        real_sentence.append(' '.join(real_part))
    real_content.append(', '.join(real_sentence))

print('\n'.join(real_content))


扫描二维码,在手机上阅读
收藏
请先 登录 再评论