gpt4 book ai didi

python - 提高列表连接的速度?

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:27 28 4
gpt4 key购买 nike

我在一个循环中有一个名为 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 吗?谢谢。

最佳答案

在以性能为导向的代码中,将 2 个字符串加在一起不是一个好主意,最好使用 "".join(_items2join_) 代替。 (我在那里找到了一些基准:http://www.skymind.com/~ocrow/python_string/)

关于python - 提高列表连接的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6877528/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com