Python獲得一篇文件的不重複詞列表
阿新 • • 發佈:2018-12-22
def loadDataSet(): postingList = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'], ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'], ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'], ['stop', 'posting', 'stupid', 'worthless', 'garbage'], ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'], ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']] return postingList def createVocabList(dataSet): vocabSet = set([]) # 建立空集合 for document in dataSet: vocabSet = vocabSet | set(document) # 取並集 return list(vocabSet) word = loadDataSet() word_set = createVocabList(word) print(word_set)
輸出:(可以看到輸出沒有重複詞彙)
['stop', 'not', 'stupid', 'how', 'food', 'him', 'posting', 'worthless', 'I', 'has', 'please', 'dalmation', 'licks', 'problems', 'help', 'garbage', 'buying', 'maybe', 'my', 'to', 'quit', 'flea', 'so', 'mr', 'dog', 'park', 'is', 'love', 'steak', 'ate', 'take', 'cute']