Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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/3/arrays/12.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_Arrays_String_Algorithm_Performance - Fatal编程技术网

在python中获取子字符串的更快方法?

在python中获取子字符串的更快方法?,python,arrays,string,algorithm,performance,Python,Arrays,String,Algorithm,Performance,我正在编写Skew算法来构造后缀数组,我有一个长字符串(长度>=4000)。 在Skew算法中,我必须构造三元组数组和子字符串数组 例如:我有一个字符串s='abcddd' 三元组数组是:“abc”、“bcd”、“cdd”、“ddd” 子字符串数组是:'abcddd','bcddd','cddd','ddd','dd','d' 这是我的解决方案: import numpy as np # example string = 'abdcb.....' (length >= 4000) t

我正在编写Skew算法来构造后缀数组,我有一个长字符串(长度>=4000)。 在Skew算法中,我必须构造三元组数组子字符串数组

例如:我有一个字符串
s='abcddd'

  • 三元组数组是:
    “abc”、“bcd”、“cdd”、“ddd”
  • 子字符串数组是:
    'abcddd','bcddd','cddd','ddd','dd','d'
这是我的解决方案:

import numpy as np

# example
string = 'abdcb.....' (length >= 4000)
temp = 'abdcb......###' (length >= 4000)

triples_arr = np.array([])
sub_strings = np.array([])

for i in range (0, len(temp) - 3):
    triples_arr = np.append(triples_arr, temp[i:i + 3])
    sub_strings = np.append(sub_strings, string[i:string_len])
对于长字符串(长度>=4000),需要一分钟才能完成


是否有任何解决方案可以缩短该任务的处理时间

使用理解,可以比使用
for
循环更快地构造这些字符串:

代码: 测试代码: 结果:
这可能适用于您,也可能不适用于您,但如果您对
字节
内存视图
对象而不是字符串对象进行操作,许多优化都会变得可用。例如,切片MemoryView非常便宜。

没有任何外部库和循环又如何

Triples_Array=[]
Sub_strings=[]

def hello(data):
    if not data:
        return 0
    triple=data[:3]
    Sub_strings.append(data)
    if len(triple)==3:
        Triples_Array.append(triple)



    return hello(data[1:])
print(hello('abcddd'))

print(Sub_strings)
print(Triples_Array)
输出:

['abcddd', 'bcddd', 'cddd', 'ddd', 'dd', 'd']
['abc', 'bcd', 'cdd', 'ddd']

我认为在构造后缀数组时,你不会复制所有的后缀,而是使用一些更轻量级的东西,比如起始位置来表示后缀倾斜算法,我需要三个后缀来排序和标记字母表中的字符。@stephernauch的解决方案解决了我的问题。我认为构建三元组阵列很好。但是构建子_字符串数组需要n^2的空间和时间,这与在线性时间中构建后缀数组的目的背道而驰。
['abc', 'bcd', 'cde', 'def', 'efg', 'fgh', 'ghi', 'hij', 'ijk', 'jkl',
 'klm', 'lmn', 'mno', 'nop', 'opq', 'pqr', 'qrs', 'rst', 'stu', 'tuv',
 'uvw', 'vwx', 'wxy', 'xyz']
['abcdefghijklmnopqrstuvwxyz', 'bcdefghijklmnopqrstuvwxyz',
 'cdefghijklmnopqrstuvwxyz', 'defghijklmnopqrstuvwxyz',
 'efghijklmnopqrstuvwxyz', 'fghijklmnopqrstuvwxyz',
 'ghijklmnopqrstuvwxyz', 'hijklmnopqrstuvwxyz', 'ijklmnopqrstuvwxyz',
 'jklmnopqrstuvwxyz', 'klmnopqrstuvwxyz', 'lmnopqrstuvwxyz',
 'mnopqrstuvwxyz', 'nopqrstuvwxyz', 'opqrstuvwxyz', 'pqrstuvwxyz',
 'qrstuvwxyz', 'rstuvwxyz', 'stuvwxyz', 'tuvwxyz', 'uvwxyz',
 'vwxyz', 'wxyz', 'xyz', 'yz', 'z']
Triples_Array=[]
Sub_strings=[]

def hello(data):
    if not data:
        return 0
    triple=data[:3]
    Sub_strings.append(data)
    if len(triple)==3:
        Triples_Array.append(triple)



    return hello(data[1:])
print(hello('abcddd'))

print(Sub_strings)
print(Triples_Array)
['abcddd', 'bcddd', 'cddd', 'ddd', 'dd', 'd']
['abc', 'bcd', 'cdd', 'ddd']