python 如何实现并行查找关键字所在的行?
高洛峰
高洛峰 2017-04-17 17:45:22
0
3
1077

我有几十万个关键字放在文件4.txt中,想提取文件3.txt中含有关键字的行,保存到文件5.txt中.
文件3有200万行,我使用下面的代码可以实现我的要求,但是非常慢,一个下午还没运行完,谁有快一点的方法?
使用并行改如何改造呢?我看到这里有个并行的帖子,,与我的不同的事,我要同时读以及查询同一个文件,上述链接可以并行操作多个文件。

with open('3.txt', 'r') as f3, open('4.txt', 'r') as f4, open('result.txt', 'w') as f5:
    a = [line.strip() for line in f4.readlines()]
    for li in f3.readlines():
        new_line = li.strip().split()[1][:-2]
        for i in a:
            if i in new_line:
                f5.writelines(li)
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回覆(3)
阿神

因為沒有實際的文件,沒有辦法給你一個百分之百的保證,不過對於你的 code,我有一些些效率改進上的建議:

(也許你會發現改進後的程式碼根本不需要使用並行的解決的方案)


首先一個很大的問題是readlines(),這個方法會一口氣讀取file objects 中的所有行,這對於效率和資源的使用顯然是極差的,幾十萬行幾百萬行的東西要一口氣唸了,這可是非常恐怖的. readlines(),這個方法會一口氣讀取 file objects 中的所有行,這對於效率和資源的使用顯然是極差的,幾十萬行幾百萬行的東西要一口氣讀了,這可是非常恐怖的.

詳細的分析和討論請參考Never call readlines() on a file

(文章中的這段話幾乎可當作是警語了)

There are hundreds of questions on places like StackOverflow about the readlines method, and in every case, the answer is the same.
"My code is takes forever before it even gets started, but it's pretty fast once it gets going."
That's because you're calling readlines.
"My code seems to be worse than linear on the size of the input, even though it's just a simple loop."
That's because you're calling readlines.
"My code can't handle giant files because it runs out of memory."
That's because you're calling readlines.

結論是: 建議所有使用 readlines 的地方全部改掉

範例:

with open('XXX', 'r') as f:
    for line in f.readlines():
       # do something...

一律改成:

with open('XXX', 'r') as f:
    for line in f:
       # do something...

直覺上效率會好很多.


其次,你使用了 list 來查找關鍵字,這也是相當沒效率的:

for i in a:
    if i in new_line:

為了確認 new_line 中是否有關鍵字 i,這邊走訪了一整個關鍵字 list: a,對於一般的情況可能還好,但是數十萬的關鍵字比對,對每一行都走訪一次 a 會造成大量的時間浪費,假設 a 裡面有 x 個關鍵字,f3 中有 y 行,每行有 z 個字,這邊要花的時間就是 x*y*z(根據你文件的行數,這個數量級極為驚人).

如果簡單地利用一些使用 hash 來查找的容器肯定會好一些,比如說 dictionary 或是 set


最後是關於你的查找部分:

for li in f3.readlines():
    new_line = li.strip().split()[1][:-2]
    for i in a:
        if i in new_line:
            f5.writelines(li)

這邊我不是很懂,new_line 看起來是一個子字串,然後現在要用這個字串去比對關鍵字?

不過先撇開這個不談,關於含有關鍵字的 new_line 在印出後,似乎不該繼續循環 a,除非你的意思是 new_line 中有幾個關鍵字我就要印 line 幾次. 否則加上一個 break

詳細的分析與討論請參考Never call readlines() on a file

(文章中的這段話幾乎可當作是警語了)

There are hundreds of questions on places like StackOverflow about the readlines method, and in every case, the answer is the same.
"My code is takes forever before it even gets ited, takes 電話gets going."
That's because you're calling readlines.
"My code seems to be worse than linear on the size of the input, even though it's just a simple loop."
That's because you're calling readlines.
"My code can't handle giant files because it runs out of memory."
That's because you're calling readlines.

🎜結論是: 建議所有使用 readlines 的地方全部改掉. 🎜 🎜範例:🎜
with open('3.txt') as f3, open('4.txt') as f4, open('result.txt', 'w') as f5:
    keywords = set(line.strip() for line in f4)
    for line in f3:
        new_line = line.strip().split()[1][:-2]
        for word in new_line:
            if word in keywords:
                print(line, file=f5)
                break
🎜一律改成:🎜 rrreee 🎜直覺上效率會好很多. 🎜 🎜 🎜其次,你使用了 list 來找出關鍵字,這也是相當沒效率的:🎜 rrreee 🎜為了確認new_line 中是否有關鍵字i,這邊走訪了一整個關鍵字list: a,對於一般的情況可能還好,但是數十萬的關鍵字比對,對每一行都走訪一次a 會造成大量的時間浪費,假設a 裡面有x 個關鍵字,f3 中有y 行,每行有z 個字,這邊要花的時間就是x*y*z(根據你檔案的行數,這個數量級極為驚人). 🎜 🎜如果簡單地利用一些使用 hash 來查找的容器肯定會好一些,比如說 dictionary 或是 set. 🎜 🎜 🎜最後是關於你的查找部分:🎜 rrreee 🎜這邊我不是很懂,new_line 看起來是一個子字串,然後現在要用這個字串去比對關鍵字? 🎜 🎜不過先撇開這個不談,關於含有關鍵字的new_line 在印出後,似乎不該繼續循環a,除非你的意思是new_line 中有幾個關鍵字我就要印line 幾次. 否則加上一個 break 也是可以加快速度. 🎜 🎜 🎜建議你的code改為:🎜 rrreee 🎜如果我有弄錯你的意思,歡迎跟我說,我們再來討論一下,直覺上應該不必使用到並行就可以解決你的問題🎜
伊谢尔伦

ac自動機

黄舟

根據@dokelung的答案,稍做修改,基本能達到我的要求。這個答案與使用 grep -f 4.txt 3.txt > 5.txt的有些不同,我在比較兩個結果檔案不同點在什麼地方。

with open('3.txt') as f3, open('4.txt') as f4, open('result.txt', 'w') as f5:
    keywords = set(line.strip() for line in f4)
    for line in f3:
        new_line = line.strip().split()[1][:-2]
        if new_line in keywords:
            print(line.strip(), file=f5)
            
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!