Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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_String_Binary - Fatal编程技术网

Python—组合一系列字符串的最快方法

Python—组合一系列字符串的最快方法,python,string,binary,Python,String,Binary,我有一个很大的列表,名为索引。列表中的每个项都是一个包含2个整数的元组。整数表示一系列子字符串的起始索引和结束索引,我想将这些子字符串连接成一个更大的字符串 例如,如果索引列表只有两个类似的条目:[(1,5)、(10,15)]我希望有一个由索引位置1和5之间的字符以及索引位置10-15之间的字符组成的输出字符串。。。。也就是说,将这两个切片连接在一起 更具体地说,我的主字符串实际上是大量十六进制值的字符串版本。我将最终将我的最终字符串转换回二进制版本,然后将其写入文件。目前,我的处理大型数据集的

我有一个很大的列表,名为
索引
。列表中的每个项都是一个包含2个整数的元组。整数表示一系列子字符串的起始索引和结束索引,我想将这些子字符串连接成一个更大的字符串

例如,如果
索引
列表只有两个类似的条目:
[(1,5)、(10,15)]
我希望有一个由索引位置1和5之间的字符以及索引位置10-15之间的字符组成的输出字符串。。。。也就是说,将这两个切片连接在一起

更具体地说,我的主字符串实际上是大量十六进制值的字符串版本。我将最终将我的最终字符串转换回二进制版本,然后将其写入文件。目前,我的处理大型数据集的速度非常慢,如下所示:

def combineFrames(indexes, largeString):
    '''
    input1 = list of all indexes for start/end of substring
    input2 = the larger String from which the substrings are being pulled

    returns a single string that consists of all substrings concatenated together...

    function also generates a new file that consists of the binary version of the concatenated substrings
    '''

    final_string = ""

    for seq in indexes:
        final_string += hexString[seq[0]:seq[1]]

    fullFile = binascii.unhexlify(final_string)

    with open("d:\\output_File", 'wb') as f:
        f.write(fullFile)

    return fullFile
final_string = "".join(hexString[seq[0]:seq[1]] for seq in indexes)
目前,上述情况非常缓慢,我怀疑有办法加快速度。思考?

使用通常是一种很好的方法来处理这样的字符串。可能看起来像这样:

def combineFrames(indexes, largeString):
    '''
    input1 = list of all indexes for start/end of substring
    input2 = the larger String from which the substrings are being pulled

    returns a single string that consists of all substrings concatenated together...

    function also generates a new file that consists of the binary version of the concatenated substrings
    '''

    final_string = ""

    for seq in indexes:
        final_string += hexString[seq[0]:seq[1]]

    fullFile = binascii.unhexlify(final_string)

    with open("d:\\output_File", 'wb') as f:
        f.write(fullFile)

    return fullFile
final_string = "".join(hexString[seq[0]:seq[1]] for seq in indexes)
您可以
“”。加入
方法是因为:

''.join通常比使用+,因为 “a”+“b”+“c”还将创建中间字符串“ab”,并且仅 然后是“abc”,而“”。join(['a','b','c'])将直接创建 “abc”。使用更少的内存,速度更快


“真的很慢”有多慢?什么尺寸的输入?多快才算足够快?你分析过这些吗?我是一个完全不懂编码的人——只做了几个月,所以我不确定还有什么其他选择或者如何分析这些。。。之所以提出这个问题,是因为有一种假设,即遍历每个索引可能不是处理它的最佳方式,而且我在搜索过程中找不到其他方式。有没有更好的方法,我可以问这个问题(仅供将来参考),主要是自己尝试回答其中一些问题-它现在运行得有多快?输入的大小/类型是什么?目标是什么?评测可以帮助识别代码中占用时间的部分(您可以在谷歌上搜索更长的主题),但最初,仅测量运行时间是一个好的开始。另一个好的实践是简要描述您正在解决的一般问题,因为有时存在完全不同的总体优化方法。一般来说,对于任何与性能相关的工作,无论是否是一个问题,越具体的度量和目标越好。这比我迭代每个片段的方式要快得多。非常感谢你。现在是时候让我去阅读一下join方法了:)