简体   繁体   English

提高列表连接的速度?

[英]Improve speed for list concatenation?

I have a list called L inside a loop that must iterate though millions of lines. 我在循环中有一个名为L的列表,它必须遍历数百万行。 The salient features are: 突出的特点是:

for line in lines:
    L = ['a', 'list', 'with', 'lots', 'of', 'items']
    L[3] = 'prefix_text_to_item3' + L[3]
    Do more stuff with L...

Is there a better approach to adding text to a list item that would speed up my code. 有没有更好的方法将文本添加到列表项,这将加快我的代码。 Can .join be used? 可以使用.join吗? Thanks. 谢谢。

In a performance oriented code, it is not a good idea to add 2 strings together, it is preferable to use a "".join(_items2join_) instead. 在面向性能的代码中,将2个字符串组合在一起并不是一个好主意,最好使用"".join(_items2join_) (I found some benchmarks there : http://www.skymind.com/~ocrow/python_string/ ) (我在那里找到了一些基准: http//www.skymind.com/~ocrow/python_string/

Since accessing an element in a python list is O(1), and appending a list to another is O(1) (which is probably the time complexity of concatenating strings in python), The code you have provided is running as fast as it can as far as I can tell. 因为访问python列表中的元素是O(1),并且将列表附加到另一个是O(1)(这可能是在python中连接字符串的时间复杂度),所提供的代码运行速度与它一样快可以据我所知。 :) You probably can't afford to do this, but when I need speed I go to C++ or some other compiled language when I need to process that much information. :)你可能负担不起,但是当我需要速度时,我需要处理那么多信息时使用C ++或其他一些编译语言。 Things run much quicker. 事情发展得更快。 For time complexity of list operations in python, you may consult this web site: http://wiki.python.org/moin/TimeComplexity and here: What is the runtime complexity of python list functions? 对于python中列表操作的时间复杂度,您可以访问以下网站: http//wiki.python.org/moin/TimeComplexity ,这里: python列表函数的运行时复杂性是多少?

Don't actually create list objects. 实际上不要创建列表对象。

Use generator functions and generator expressions. 使用生成函数和生成器表达式。

def appender( some_list, some_text ):
    for item in some_list:
        yield item + some_text

This appender function does not actually create a new list. appender函数实际上不会创建新列表。 It avoids some of the memory management overheads associated with creating a new list. 它避免了与创建新列表相关的一些内存管理开销。

There may be a better approach depending on what you are doing with list L. 根据您对列表L的处理方式,可能有更好的方法。

For instance, if you are printing it, something like this may be faster. 例如,如果您正在打印它,这样的事情可能会更快。

print "{0} {1} {2} {3}{4} {5}".format(L[0], L[1], L[2], 'prefix_text_to_item3', L[3], L[4]) 打印“{0} {1} {2} {3} {4} {5}”。format(L [0],L [1],L [2],'prefix_text_to_item3',L [3],L [ 4])

What happens to L later in the program? L后来在程序中会发生什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM