提高列表连接的速度?(Improve speed for list concatenation?)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
提高列表连接的速度?(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/)

更多推荐

本文发布于:2023-04-17 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/f7032e24290a0db46a8a83a6388b8139.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度   列表   Improve   concatenation   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!