迭代与列表串联 [英] Iterating vs List Concatenation

查看:67
本文介绍了迭代与列表串联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有两种方法可以获取一个列表并将第二个列表的成员添加到第一个列表中.您可以使用列表连接,也可以对其进行迭代.您可以:

 for obj in list2:list1.append(obj)

或者你可以:

list1 = list1 + list2

list1 += list2

我的问题是:哪个更快,为什么?我使用两个非常大的列表(超过 10000 个对象)对此进行了测试,似乎迭代方法比列表连接(如 l1 = l1 + l2)快得多.为什么是这样?有人可以解释一下吗?

解决方案

append 每次添加一个项,这是导致它缓慢的原因,以及对<的重复函数调用代码>附加.

然而在这种情况下,+= 运算符不是+ 的语法糖.+= 运算符实际上并没有创建一个新列表然后将其分配回来,它会在适当的位置修改左侧操作数.当使用 timeit 使用 10,000 次时很明显.

<预><代码>>>>timeit.timeit(stmt="l = l + j", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)0.5794978141784668>>>timeit.timeit(stmt="l += j", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)0.0013298988342285156

+= 快得多(大约 500 倍)

你也有用于列表的 extend 方法,它可以附加任何可迭代的(不仅仅是另一个列表),比如 l.extend(l2)

<预><代码>>>>timeit.timeit(stmt="l.extend(j)", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)0.0016009807586669922>>>timeit.timeit(stmt="for e in j: l.append(e)", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)0.00805807113647461

逻辑上等同于追加,但如您所见,速度要快得多.

所以解释一下:迭代比 + 更快,因为 + 必须构造一个完整的新列表

extend 比迭代更快,因为它是一个内置列表方法并且已经过优化.逻辑上等同于重复追加,但实现方式不同.

+=extend 更快,因为它可以就地修改列表,知道列表必须有多大,并且无需重复函数调用.它假设您将列表附加到另一个列表/元组

So there are two ways to take a list and add the members of a second list to the first. You can use list concatenation or your can iterate over it. You can:

for obj in list2:
    list1.append(obj)

or you can:

list1 = list1 + list2

or

list1 += list2

My question is: which is faster, and why? I tested this using two extremely large lists (upwards of 10000 objects) and it seemed the iterating method was a lot faster than the list concatenation (as in l1 = l1 + l2). Why is this? Can someone explain?

解决方案

append adds each item one at a time, which is the cause of its slowness, as well as the repeated function calls to append.

However in this case the += operator is not syntactic sugar for the +. The += operator does not actually create a new list then assign it back, it modifies the left hand operand in place. It's pretty apparent when using timeit to use both 10,000 times.

>>> timeit.timeit(stmt="l = l + j", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)
0.5794978141784668
>>> timeit.timeit(stmt="l += j", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)
0.0013298988342285156

+= is much faster (about 500x)

You also have the extend method for lists which can append any iterable (not just another list) with something like l.extend(l2)

>>> timeit.timeit(stmt="l.extend(j)", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)
0.0016009807586669922
>>> timeit.timeit(stmt="for e in j: l.append(e)", setup="l=[1,2,3,4]; j = [5,6,7,8]", number=10000)
0.00805807113647461

Logically equivalent to appending, but much much faster as you can see.

So to explain this: iterating is faster than + because + has to construct an entire new list

extend is faster than iteration because it's a builtin list method and has been optimized. Logically equivalent to appending repeatedly, but implemented differently.

+= is faster than extend because it can modify the list in place, knowing how much larger the list has to be and without repeated function calls. It assumes you're appending your list with another list/tuple

这篇关于迭代与列表串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆