追加 python

Append in python

我想打开一个文件,逐行阅读。对于每一行,我想使用 split() 方法将该行拆分为一个单词列表。然后我想检查每一行上的每个单词,看看该单词是否已经在列表中,如果没有,则将其附加到列表中。这是我写的代码。

fname = raw_input("Enter file name: ")
fh = open(fname)
line1 = list()
for line in fh:
    stuff = line.rstrip().split()
    for word in stuff:
        if stuff not in stuff:
            line1.append(stuff)
print line1

我的问题是,当我打印第 1 行时,它会以这样的格式打印出大约 30 个重复列表。

['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 'breaks'], 
['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 'breaks'], ['It', 'is', 'the', 'east', 'and', 'Juliet', 'is', 'the', 'sun'], 
    ['It', 'is', 'the', 'east', 'and', 'Juliet', 'is', 'the', 'sun']
    ['Arise', 'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon'], 
    ['Arise', 'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon'],

我想知道为什么会出现这个问题,以及如何删除重复的单词和列表。

你有 if stuff not in stuff。如果您将该行更改为 if word not in line1: 并将下一行更改为 line1.append(word) 您的代码应该可以工作。

或者,使用集合。

fname = raw_input("Enter file name: ")
fh = open(fname)
line1 = set()
for line in fh:
    stuff = line.rstrip().split()
    for word in stuff:
        line1.add(word)
print line1

甚至

fname = raw_input("Enter file name: ")
fh = open(fname)
line1 = set()
for line in fh:
    stuff = line.rstrip().split()
    line1 = line1.union(set(stuff))
print line1

集合将只包含唯一值(尽管它们没有排序或索引的概念),因此您无需处理检查单词是否已经出现的问题:集合数据类型会自动处理。