这是我正在尝试的代码:
file1 = open("romeo.txt")
list_orig=[]
list_distinct=[]
for line in file1:
words=line.split()
list_orig=list_orig+words
for word in words:
if word in list_distinct:continue
list_distinct= list_distinct+word
print (list_distinct)
您能帮我改正代码吗?没有得到想要的结果。
我正在寻求帮助来比较 romeo.txt 文件中的单词并将每个单词与现有单词列表进行比较。 (如果列表中存在单词,则丢弃它,如果单词不存在,则添加它。)打印排序列表。
一个简单的方法是用不同的单词填充列表:
file1 = open("romeo.txt")
list_existing=['one']
list_distinct=[]
for line in file1:
words=line.split()
for word in words:
if word not in list_existing:
list_distinct.append(word)
print (list_distinct)
在此示例中,romeo.txt 是:
this is a test
one
则输出为:
['this', 'is', 'a', 'test']
简短而简单的修复:您只能连接列表。
# ...
list_distinct = list_distinct + [word]
然而,当您不断创建新的列表对象时,该方法是有问题的。最好继续扩大同一个列表:
list_distinct.append(word)
更好的是,使用集合或字典进行成员资格测试。它的性能 (O(1)
) 比列表 (
O(N)
) 好得多。整个原理可以简化为:
file1 = open("romeo.txt")
# build list in one go!
list_orig = [w for line in file1 for w in line.split()]
# build list of unique elements in order of occurrence
list_distinct = [*dict.fromkeys(list_orig)] # dict maintains insertion order
print(list_distinct)
with open("romeo.txt", mode='r') as f:
data = f.readlines()
list_distinct = []
for line in data:
words = line.split()
for word in words:
if word not in list_distinct:
list_distinct.append(word)
print(list_distinct)