如何创建一种更有效的方法来解析两个大文本文件之间的单词(Python 3.6.4)

火星

我是 Python 的新手,这是我第一次尝试应用我所学的知识,但我知道我效率低下。该代码有效,但需要几分钟才能完成对一个新颖大小的文本文件的执行。

有没有更有效的方法来达到相同的输出?任何造型批评也将不胜感激。谢谢!

def realWords(inFile, dictionary, outFile):
    with open(inFile, 'r') as inf, open(dictionary, 'r') as dictionary, open(outFile, 'w') as outf:
    realWords = ''
    dList = []
    for line in dictionary:
        dSplit = line.split()
        for word in dSplit:
            dList.append(word)
    for line in inf:
        wordSplit = line.split()
        for word in wordSplit:
            if word in dList:
                realWords += word + ' '
    outf.write(realWords)
    print('File of real words created')
    inf.close()
    dictionary.close()
    outf.close()

'''
I created a function to compare the words in a text file to real words taken 
from a reference dictionary (like the Webster Unabridged Dictionary). It 
takes a text file and breaks it up into individual word components. It then 
compares each word to each word in the reference dictionary text file in 
order to test whether the world is a real word or not. This is done so as to 
eliminate non-real words, names, and some other junk. For each word that 
passes the test, each word is then added to the same empty string. Once all 
words have been parsed, the output string containing all real words is 
written to a new text file.
'''
普里姆萨

对于小说中的每个单词,您都可以在整个词典中搜索一次,看看是否能找到该单词。这真的很慢。

您可以从使用 set() 数据结构中受益,它可以让您在恒定时间内快速确定元素是否在其中。

此外,通过摆脱字符串连接并使用 .join() 代替,您可以进一步加快代码速度。

我对你的代码做了一些调整,所以它使用了 set() 和 .join(),这应该会大大加快速度

def realWords(inFile, dictionary, outFile):
    with open(inFile, 'r') as inf, open(dictionary, 'r') as dictionary, open(outFile, 'w') as outf:
    realWords = [] #note list for constant time appends
    dList = set()
    for line in dictionary:
        dSplit = line.split()
        for word in dSplit:
        dList.add(word)
    for line in inf:
        wordSplit = line.split()
        for word in wordSplit:
            if word in dList: #done in constant time because dList is a set
                realWords.append(word)
    outf.write(' '.join(realWords))
    print('File of real words created')
    inf.close()
    dictionary.close()
    outf.close()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery从文本“ 2-6”创建数组,例如[2,3,4,5,6]

来自分类Dev

vector <int> + = 1,1,2,2,2,3,4,5,6如何; 可能的?

来自分类Dev

随机数网格4 x 6 Python 3

来自分类Dev

匹配5'2“,6'和2'-4”,但不匹配3''(两个撇号)的正则表达式

来自分类Dev

R:如何制作序列(1,1,1,2,3,3,3,4,5,5,5,6,7,7,7,8)

来自分类Dev

d3 v4 + React + ES6:如何以编程方式创建轴?

来自分类Dev

R-在typeof()函数的情况下c(1:6)和c(1,2,3,4,5,6)之间的差?

来自分类Dev

Neo4j是否有一种更有效的方式来编写多个where-contains?

来自分类Dev

如何在python中打开格式为[[skill1,skill2,skill3,...],[skill1,skill4,skill6,...],....]的大文本文件。

来自分类Dev

如何重复序列:r中的1,2,3,4,5,6,1,2,3,4,5,6,7,8,9,10,7,8,9,10

来自分类Dev

NumPy:在两个4D矩阵中将每对3D矩阵相乘的有效方法吗?

来自分类Dev

RuntimeError:给定组= 1,权重为[32,3,3,3],预期输入[4,32,6,7]具有3个通道,但改为有32个通道

来自分类Dev

有人可以解释为什么此代码返回6 6 6 6 6而不是1 2 3 4 5

来自分类Dev

需要一种更有效的方式来合并文本文件

来自分类Dev

交换以2的[1,2,3,4,5,6] = [2,1,4,3,6,5]分组的相邻元素

来自分类Dev

jQuery从文本“ 2-6”创建数组,例如[2,3,4,5,6]

来自分类Dev

3节,4节和6节电池有什么区别

来自分类Dev

如何在R中将c(1,2,3)和c(4,5,6)连接到c(1,4,2,5,3,6)?

来自分类Dev

OpenMDAO ShelveRecorder以一种奇怪的顺序保存迭代:第1次迭代,第3次,第2次,第5次,第4次,第6次

来自分类Dev

Cocoapods无法正确链接第3个,第4个目标(Xcode 7 beta 6)

来自分类Dev

C#-有一种方法可以缩短“ if(ab == 1 || ab == 4 || ab == 6)”

来自分类Dev

如何转换list1中的list = [1,2,[3,4],[5,6],7,[8,9,10]] = [1,2,3,4,5,6,7,8 ,9,10]在python中?

来自分类Dev

匹配5'2“,6'和2'-4”,但不匹配3''(两个撇号)的正则表达式

来自分类Dev

R:如何制作序列(1,1,1,2,3,3,3,4,5,5,5,6,7,7,7,8)

来自分类Dev

有没有一种真正有效(FAST)的方法来读取python中的大文本文件?

来自分类Dev

4,5,6 ... 3D图上的尺寸数据?

来自分类Dev

如何将版本 6 的 LibreOffice Impress 演示文稿从 4:3 更改为 16:9?

来自分类Dev

如何在python中使用optparse解析有效的IP4/IPV6地址?

来自分类Dev

如何使用张量流将 [1,2,3,4,5,6] 重塑为 [[1,3,5],[2,4,6]]?

Related 相关文章

  1. 1

    jQuery从文本“ 2-6”创建数组,例如[2,3,4,5,6]

  2. 2

    vector <int> + = 1,1,2,2,2,3,4,5,6如何; 可能的?

  3. 3

    随机数网格4 x 6 Python 3

  4. 4

    匹配5'2“,6'和2'-4”,但不匹配3''(两个撇号)的正则表达式

  5. 5

    R:如何制作序列(1,1,1,2,3,3,3,4,5,5,5,6,7,7,7,8)

  6. 6

    d3 v4 + React + ES6:如何以编程方式创建轴?

  7. 7

    R-在typeof()函数的情况下c(1:6)和c(1,2,3,4,5,6)之间的差?

  8. 8

    Neo4j是否有一种更有效的方式来编写多个where-contains?

  9. 9

    如何在python中打开格式为[[skill1,skill2,skill3,...],[skill1,skill4,skill6,...],....]的大文本文件。

  10. 10

    如何重复序列:r中的1,2,3,4,5,6,1,2,3,4,5,6,7,8,9,10,7,8,9,10

  11. 11

    NumPy:在两个4D矩阵中将每对3D矩阵相乘的有效方法吗?

  12. 12

    RuntimeError:给定组= 1,权重为[32,3,3,3],预期输入[4,32,6,7]具有3个通道,但改为有32个通道

  13. 13

    有人可以解释为什么此代码返回6 6 6 6 6而不是1 2 3 4 5

  14. 14

    需要一种更有效的方式来合并文本文件

  15. 15

    交换以2的[1,2,3,4,5,6] = [2,1,4,3,6,5]分组的相邻元素

  16. 16

    jQuery从文本“ 2-6”创建数组,例如[2,3,4,5,6]

  17. 17

    3节,4节和6节电池有什么区别

  18. 18

    如何在R中将c(1,2,3)和c(4,5,6)连接到c(1,4,2,5,3,6)?

  19. 19

    OpenMDAO ShelveRecorder以一种奇怪的顺序保存迭代:第1次迭代,第3次,第2次,第5次,第4次,第6次

  20. 20

    Cocoapods无法正确链接第3个,第4个目标(Xcode 7 beta 6)

  21. 21

    C#-有一种方法可以缩短“ if(ab == 1 || ab == 4 || ab == 6)”

  22. 22

    如何转换list1中的list = [1,2,[3,4],[5,6],7,[8,9,10]] = [1,2,3,4,5,6,7,8 ,9,10]在python中?

  23. 23

    匹配5'2“,6'和2'-4”,但不匹配3''(两个撇号)的正则表达式

  24. 24

    R:如何制作序列(1,1,1,2,3,3,3,4,5,5,5,6,7,7,7,8)

  25. 25

    有没有一种真正有效(FAST)的方法来读取python中的大文本文件?

  26. 26

    4,5,6 ... 3D图上的尺寸数据?

  27. 27

    如何将版本 6 的 LibreOffice Impress 演示文稿从 4:3 更改为 16:9?

  28. 28

    如何在python中使用optparse解析有效的IP4/IPV6地址?

  29. 29

    如何使用张量流将 [1,2,3,4,5,6] 重塑为 [[1,3,5],[2,4,6]]?

热门标签

归档