Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python文本文件读取速度很慢_Python_File_Pattern Matching - Fatal编程技术网

python文本文件读取速度很慢

python文本文件读取速度很慢,python,file,pattern-matching,Python,File,Pattern Matching,如何使这个python程序更快地读取大文本文件?我的代码几乎需要五分钟来读取文本文件,但我需要它更快地读取文本文件。我认为我的算法不在O(n)中 一些示例数据(数据量为470K+行): 我的代码: import string import re WORDLIST_FILENAME = "words.txt" def load_words(): wordlist = [] print("Loading word list from file...") with open(WORDLIS

如何使这个python程序更快地读取大文本文件?我的代码几乎需要五分钟来读取文本文件,但我需要它更快地读取文本文件。我认为我的算法不在O(n)中

一些示例数据(数据量为470K+行):

我的代码:

import string
import re


WORDLIST_FILENAME = "words.txt"

def load_words():
 wordlist = []
 print("Loading word list from file...")
 with open(WORDLIST_FILENAME, 'r') as f:
     for line in f:
         wordlist = wordlist + str.split(line)
 print("  ", len(wordlist), "words loaded.")
 return wordlist

def find_words(uletters):
wordlist = load_words()
foundList = []

for word in wordlist:
    wordl = list(word)
    letters = list(uletters)
    count = 0
    if len(word)==7:
        for letter in wordl[:]:
            if letter in letters:
                wordl.remove(letter)
               # print("word left" + str(wordl))
                letters.remove(letter)                    
               # print(letters)
                count = count + 1
                #print(count)
                if count == 7:
                    print("Matched:" + word)
                    foundList = foundList + str.split(word)
foundList.sort()
result = ''
for items in foundList: 
      result = result + items + ','
print(result[:-1])


#Test cases
find_words("eabauea" "iveabdi")
#pattern =   "asa" " qlocved"
#print("letters to look for: "+ pattern)
#find_words(pattern)

将单列文件读入带有拆分线()的列表:

您可以使用
timeit
对其进行基准测试:

from timeit import timeit

timeit('load_words()', setup=setup, number=3)
# Output: 0.1708553659846075 seconds
至于如何实现模糊匹配算法,您可以尝试一下 :

输出:

[('-a', 90), ('A', 90), ('A.', 90), ('a', 90), ("a'", 90),
 ('a-', 90), ('a.', 90), ('AB', 90), ('Ab', 90), ('ab', 90)]
 [('abaue', 83),
 ('Ababua', 77),
 ('Abatua', 77),
 ('Bauera', 77),
 ('baulea', 77),
 ('abattue', 71),
 ('abature', 71),
 ('ablaqueate', 71),
 ('bauleah', 71),
 ('ebauche', 71),
 ('habaera', 71),
 ('reabuse', 71),
 ('Sabaean', 71),
 ('sabaean', 71),
 ('Zabaean', 71),
 ('-acea', 68)]
如果筛选较长的匹配项,结果会更有趣:

results = process.extract("eabauea", wordlist, limit=100)
[x for x in results if len(x[0]) > 4]
输出:

[('-a', 90), ('A', 90), ('A.', 90), ('a', 90), ("a'", 90),
 ('a-', 90), ('a.', 90), ('AB', 90), ('Ab', 90), ('ab', 90)]
 [('abaue', 83),
 ('Ababua', 77),
 ('Abatua', 77),
 ('Bauera', 77),
 ('baulea', 77),
 ('abattue', 71),
 ('abature', 71),
 ('ablaqueate', 71),
 ('bauleah', 71),
 ('ebauche', 71),
 ('habaera', 71),
 ('reabuse', 71),
 ('Sabaean', 71),
 ('sabaean', 71),
 ('Zabaean', 71),
 ('-acea', 68)]
但对于470K+行,确实需要一段时间:

timeit('process.extract("eabauea", wordlist, limit=3)', setup=setup, number=3)
# Output: 384.97334043699084 seconds

将单列文件读入带有拆分线()的列表:

您可以使用
timeit
对其进行基准测试:

from timeit import timeit

timeit('load_words()', setup=setup, number=3)
# Output: 0.1708553659846075 seconds
至于如何实现模糊匹配算法,您可以尝试一下 :

输出:

[('-a', 90), ('A', 90), ('A.', 90), ('a', 90), ("a'", 90),
 ('a-', 90), ('a.', 90), ('AB', 90), ('Ab', 90), ('ab', 90)]
 [('abaue', 83),
 ('Ababua', 77),
 ('Abatua', 77),
 ('Bauera', 77),
 ('baulea', 77),
 ('abattue', 71),
 ('abature', 71),
 ('ablaqueate', 71),
 ('bauleah', 71),
 ('ebauche', 71),
 ('habaera', 71),
 ('reabuse', 71),
 ('Sabaean', 71),
 ('sabaean', 71),
 ('Zabaean', 71),
 ('-acea', 68)]
如果筛选较长的匹配项,结果会更有趣:

results = process.extract("eabauea", wordlist, limit=100)
[x for x in results if len(x[0]) > 4]
输出:

[('-a', 90), ('A', 90), ('A.', 90), ('a', 90), ("a'", 90),
 ('a-', 90), ('a.', 90), ('AB', 90), ('Ab', 90), ('ab', 90)]
 [('abaue', 83),
 ('Ababua', 77),
 ('Abatua', 77),
 ('Bauera', 77),
 ('baulea', 77),
 ('abattue', 71),
 ('abature', 71),
 ('ablaqueate', 71),
 ('bauleah', 71),
 ('ebauche', 71),
 ('habaera', 71),
 ('reabuse', 71),
 ('Sabaean', 71),
 ('sabaean', 71),
 ('Zabaean', 71),
 ('-acea', 68)]
但对于470K+行,确实需要一段时间:

timeit('process.extract("eabauea", wordlist, limit=3)', setup=setup, number=3)
# Output: 384.97334043699084 seconds

听起来更适合。如果你也能解释一下你的程序应该做什么,这会有帮助。有一件事
wordlist=wordlist+str.split(line)
每行复制单词列表。执行
wordlist.extend(line.strip().split())
。或者,如果您想消除重复项并加快单词查找速度,请将
wordlist
改为
set
,然后执行
.update
。您有两个for循环,一个是每次进行浅拷贝,并从两个列表中删除项目。加上检查中的
。还取决于文件的大小。您还遍历了整个文件,然后再次遍历了内容。解释一下你想做什么会很有帮助。你能提供一个“words.txt”用于测试吗?听起来更适合。如果你也能解释一下你的程序应该做什么,那会很有帮助。有一件事
wordlist=wordlist+str.split(line)
每行复制单词列表。执行
wordlist.extend(line.strip().split())
。或者,如果您想消除重复项并加快单词查找速度,请将
wordlist
改为
set
,然后执行
.update
。您有两个for循环,一个是每次进行浅拷贝,并从两个列表中删除项目。加上
检查中的
。还取决于文件的大小。您还遍历了整个文件,然后再次遍历了内容。解释一下你想做什么会很有帮助。你能提供一个“words.txt”用于测试吗?