1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
將單詞從數(shù)據(jù)庫(kù)保存到第一個(gè)單詞,set然后對(duì)其應(yīng)用str.strip和應(yīng)用str.lower。str.strip將刪除開(kāi)頭和結(jié)尾的空白字符,例如'\n'.etc。
集合提供O(1)查找,并且集合相交將比您當(dāng)前的O(n^2)方法效率更高。
然后遍歷word文件中的每一行并應(yīng)用str.strip,str.lower首先在集合中搜索它。
with open(r"words.txt") as f1, open(r"text.txt", "r") as f2:
dlist = set(line.strip().lower() for line in f2) #set of words from database
for line in f1:
line = line.strip().lower() #use strip to remove '\n'
words = set(line.split()) #use split to get the words from the line
#and convert it into a set
common_words = words & dlist #use set intersection to find common words
for word in common_words:
*trick*
請(qǐng)?zhí)鎿Qf1并f2適當(dāng)?shù)靥鎿Q,因?yàn)槲液芾Щ竽膫€(gè)是數(shù)據(jù)庫(kù),哪個(gè)是文本數(shù)據(jù)集。
添加回答
舉報(bào)