python - 字典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')
解决方案
您可以为此使用列表:
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
推荐阅读
- angular - 使用 Git 在 Heroku 上成功部署后显示“未找到”错误
- json - 如何在 Json 的文件中包含其他文件?
- spring-boot - Spring Security OAuth2 - 资源服务器访问 HTTP 请求,客户端凭据授予类型
- flutter - 在颤动中按下时如何打印通话记录?
- java - Spring Framework Dao : type cannot be null error
- python - Python and Calabi Yau Data
- c++ - Ternar operator for 3 cases
- python - Getting 504 Gateway Time-out for Django on Ubuntu
- git - AEM 6.5 中的版本控制元数据更改
- python - AndroRAT Error while executing with python