首页 > 解决方案 > 字典python,不要写现有单词

问题描述

我希望如果 Dico.txt 中已经存在一个单词,它不会再写它。

e = input("Mots: ")
f = e.split(" ")

with open("Dico.txt", "a") as f2:
    for line in f:
        for word in line.split():
            f2.write(word + '\n')

标签: pythondictionary

解决方案


您可以为此使用列表:

e = input("Mots: ")
f = e.split(" ")
u = []

with open("Dico.txt", "a") as f2:
    for line in f:
        for word in line.split():
            if not word in u:
              f2.write(word + '\n')
              u.append(word)
            else:
              continue

推荐阅读