python高效替换嵌套数组中的字符串

标签 python arrays string nested text-files

我有一个 txt 文件,其中有数千行字符串。 每行以“#integer”格式开始,例如“#100”。

我按顺序读取txt文件(行#1、#2、#3..)并获得我想要的特定数组,其中该数组是行号和连接到这些行的其他行的集合:

数组的形式为:

[ ['#355', '#354', '#357', '#356'], ['#10043', '#10047', '#10045'], ['#1221', '#1220', '#1223', '#1222', '#1224'], [...] ]

它可以包含数百个数字。 (这是因为我有一个数字数组,并且与它们关联的更多“子项”添加到每个子数组中。)

我在执行以下函数之前已读取了我的 txt 文件,这意味着我首先读取了我的 txt 文件,提取数字,然后将其作为数组传递给 extended_Strings 函数,该函数 将每个数字替换为 txt 文件中该数字行的实际字符串

def extended_strings(matrix,base_txt):
  string_matrix = matrix #new matrix to contain our future strings
  for numset in string_matrix:
    for num in numset:
      for line in base_txt:
        results = re.findall(r'^#\d+', line) #find the line # at start of string
        if len(results) > 0  and results[0] == num: #if we have a # line that matches our # in the numset
          index = numset.index(num) #find index of line # in the numset
          numset[index] = line #if we match line #'s, we replace the line # with the actual string from the txt

  return string_matrix

我试图让这个过程更短、更高效,例如我的txt中有150,000个字符串,有数百万次使用for line in base_txt行扫描txt文件.

有什么建议吗?

最佳答案

我没有进行任何测光。但我相信这会有所帮助。 另一方面,仍有很多改进的空间。

文本.txt:

#1 This is line #00001
#2 This is line #00002
#30 This is line #00030
#35 This is line #00035
#77 This is line #00077
#101 This is line #00101
#145 This is line #00145
#1010 This is line #01010
#8888 This is line #08888
#13331 This is line #13331
#65422 This is line #65422

代码:

import re

# reo = re.compile(r'^(#\d+)\s+(.*)\n$')           # exclude line numbers in "string_matrix"
reo = re.compile(r'^((#\d+)\s+.*)\n$')             # include line numbers in "string_matrix"

def file_to_dict(file_name):
    file_dict = {}
    with open(file_name) as f:
        for line in f:
            mo = reo.fullmatch(line)
            # file_dict[mo.group(1)] = mo.group(2) # exclude line numbers in "string_matrix"
            file_dict[mo.group(2)] = mo.group(1)   # include line numbers in "string_matrix"
    return file_dict

def extended_strings(matrix, file_dict):
    string_matrix = []
    for numset in matrix:
        new_numset = []
        for num in numset:
            new_numset.append(file_dict[num])
        string_matrix.append(new_numset)
    return string_matrix


matrix = [['#1010', '#35', '#2', '#145', '#8888'], ['#30', '#2'], ['#65422', '#1', '#13331', '#77', '#101', '#8888']]

file_dict = file_to_dict('text.txt')
string_matrix = extended_strings(matrix, file_dict)
for list_ in string_matrix:
    for line in list_:
        print(line)
    print()

关于python高效替换嵌套数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239582/

相关文章:

python - 如何使用 Django 映射音频文件?

python - 在 Google-App-Engine 上使用 Django 访问和更新模型

Javascript:查找并删除*所有*重复的项目

string - 将 VBA 字符串转换为 double

Python,单元测试 - 将命令行参数传递给 unittest.TestCase 的 setUp

python - 找出总和最接近给定数字的三个数字

ios - 类型 'Any' 不符合协议(protocol) 'Sequence'

jquery - 对于 jQuery 纯粹主义者 - 简单的数组填充?

c - free() 提示无效指针

java - 如何从Java中不断变化的模板中提取字符串?