I'm looking for the fastest way to replace a large number of sub-strings inside a very large string. Here are two examples I've used.

我正在寻找一种在非常大的字符串中替换大量子字符串的最快方法。这是我用过的两个例子。

findall() feels simpler and more elegant, but it takes an astounding amount of time.

findall()感觉更简单,更优雅,但需要花费大量时间。

finditer() blazes through a large file, but I'm not sure this is the right way to do it.

finditer()通过一个大文件,但我不确定这是正确的方法。

Here's some sample code. Note that the actual text I'm interested in is a single string around 10MB in size, and there's a huge difference in these two methods.

这是一些示例代码。请注意,我感兴趣的实际文本是一个大小约10MB的单个字符串,这两种方法有很大的不同。

import re

def findall_replace(text, reg, rep):
    for match in reg.findall(text):
        output = text.replace(match, rep)
    return output

def finditer_replace(text, reg, rep):
    cursor_pos = 0
    output = ''
    for match in reg.finditer(text):
        output += "".join([text[cursor_pos:match.start(1)], rep])
        cursor_pos = match.end(1)
    output += "".join([text[cursor_pos:]])
    return output

reg = re.compile(r'(dog)')
rep = 'cat'
text = 'dog cat dog cat dog cat'

finditer_replace(text, reg, rep)

findall_replace(text, reg, rep)

UPDATE Added re.sub method to tests:

更新为测试添加了re.sub方法:

def sub_replace(reg, rep, text):
    output = re.sub(reg, rep, text)
    return output

Results

结果

re.sub() - 0:00:00.031000
finditer() - 0:00:00.109000
findall() - 0:01:17.260000

re.sub() - 0:00:00.031000 finditer() - 0:00:00.109000 findall() - 0:01:17.260000

3 个解决方案

#1


14

The standard method is to use the built-in

标准方法是使用内置的

re.sub(reg, rep, text)

Incidentally the reason for the performance difference between your versions is that each replacement in your first version causes the entire string to be recopied. Copies are fast, but when you're copying 10 MB at a go, enough copies will become slow.

顺便提一下,版本之间性能差异的原因是第一个版本中的每个替换都会导致整个字符串被重新复制。副本速度很快,但是当你一次复制10 MB时,足够的副本会变慢。

更多相关文章

  1. python笔记7:接口实现方法
  2. 【Python】Python3 字典 copy()方法
  3. jieba(结巴)Python分词器加载到Eclipse方法
  4. python,os模块的常用方法
  5. Python语言的特点、程序设计基本方法
  6. Pandas 文本数据方法 findall( )
  7. python 字符串操作
  8. python中函数参数传递的几种方法
  9. TensorFlow数据集(一)——数据集的基本使用方法

随机推荐

  1. linux环境下写C++操作mysql(一)
  2. 蜂巢和数据库之间的完整性检查
  3. sql随机获获取数据
  4. 替换wordpress WP_POSTS表中post_date字
  5. mysql的count方法详解
  6. Spring+SpringMVC+MyBatis+easyUI整合基
  7. navicat for mysql 注册码,简简单单,一个搞
  8. MySQL数据库语法-多表查询练习一
  9. MySQL - 更改一行的时间值以匹配同一表
  10. php将图片以二进制形式保存到mysql数据库