提高列表连接的速度?(Improve speed for list concatenation?)
我在循环中有一个名为L的列表,它必须遍历数百万行。 突出的特点是:
for line in lines: L = ['a', 'list', 'with', 'lots', 'of', 'items'] L[3] = 'prefix_text_to_item3' + L[3] Do more stuff with L...有没有更好的方法将文本添加到列表项,这将加快我的代码。 可以使用.join吗? 谢谢。
I have a list called L inside a loop that must iterate though millions of lines. 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? Thanks.
最满意答案
在面向性能的代码中,将2个字符串组合在一起并不是一个好主意,最好使用"".join(_items2join_) 。 (我在那里找到了一些基准: http : //www.skymind.com/~ocrow/python_string/ )
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. (I found some benchmarks there : http://www.skymind.com/~ocrow/python_string/)
更多推荐
发布评论