私はPythonを学ぼうとしています。Pythonのリストに一意の値だけを追加してください
単語が既にリストに含まれているかどうかを確認してください。 という単語がリストにない場合は、リストに追加します。
これは私が持っているものです。
fhand = open('romeo.txt')
output = []
for line in fhand:
words = line.split()
for word in words:
if word is not output:
output.append(word)
print sorted(output)
これは私が得たものです。
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'and', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'is', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'sun', 'the', 'the', 'the', 'through', 'what', 'window', 'with', 'yonder']
注重複(および、ある、太陽、など)。
ユニークな値はどのようにして取得できますか?
慣習的なやり方は、チェックする単語の*集合*を維持することです。成長リスト上のそれらの線形走査はすべて、そうでなければ線形アルゴリズムを二次的に劣化させる。 –