第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如果…in-必要時(shí)不匹配

如果…in-必要時(shí)不匹配

湖上湖 2021-03-28 14:11:39
我有一個(gè)單詞數(shù)據(jù)庫(kù)和一個(gè)帶有文本行的數(shù)據(jù)集。每當(dāng)文本文件的行中也出現(xiàn)一個(gè)單詞,并且該單詞也出現(xiàn)在單詞文件中時(shí),我想做個(gè)技巧。我的代碼如下所示:import ref = open(r"words.txt")print len(flist)d = open(r"text.txt", "r")dlist = d.readlines()for line in flist:    lowline = line.lower()    for word in dlist:        lowword = word.lower()        if lowword in lowline:            *trick*但是,此代碼未找到匹配項(xiàng),盡管有許多單詞完全相同。對(duì)這個(gè)有什么想法嗎?
查看完整描述

1 回答

?
鴻蒙傳說(shuō)

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ù)集。


查看完整回答
反對(duì) 回復(fù) 2021-04-06
  • 1 回答
  • 0 關(guān)注
  • 150 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)