我的代码能够显示文本文件中以特定字母开头的每个单词,但我希望它不显示重复的单词。这是我的代码:
with open('text.txt','r') as myFile:
data=myFile.read().lower()
for s in data.split():
if s.startswith("r"):
print(s)
就像我说的那样,我的代码确实打印了单词,但显示出重复项。谢谢你的帮助
答案 0 :(得分:2)
使用set:
with open('text.txt', 'r') as myFile:
data = myFile.read().lower()
seen = set()
for s in data.split():
if s not in seen and s.startswith("r"):
seen.add(s)
print(s)
答案 1 :(得分:0)
这是一个优化的版本,它将逐行读取文件而不将其全部加载到内存中。
seen_words = set()
with open('text.txt', 'r') as my_file:
for line in my_file:
for word in line.lower().split():
if word not in seen_words:
print(word)
seen_words.add(word)