简体   繁体   English

Python 列表切片和 += 运算符

[英]Python lists slicing and += operator

Given the below examples:鉴于以下示例:

array = [1,2,3,4,0]

In: array[0] += 2 
Out: 3

In: array[1:3] += 2  
Out: TypeError: 'int' object is not iterable

In: array[1:3] += [100, 100] 
Out: [1, 2, 3, 100, 100, 4, 5]

Can someone explain me why the two last examples wont return something like [1,102,103,4,0] AND if it is possible doing this with a simple slice, not using a for loop...有人能解释一下为什么最后两个例子不会返回类似 [1,102,103,4,0] 的东西,如果可以用一个简单的切片来做这个,而不是使用 for 循环......

When using the slice operator it refers to sub-part of the list, so operating on it requires a list too (we can use the "add" operator on list with list , and not with an int , unlike in some other languages).当使用它指的是列表的子部分的分割运算,这样就可以操作需要一个列表太(我们可以使用“添加”操作符listlist ,而不是与一个int ,不像一些其他语言)。

Therefore the following:因此有以下几点:

array[1:3] += 2

Throws:抛出:

TypeError: 'int' object is not iterable

Because 2 is not a list (actually an iterable, which is more general than list ).因为2不是一个列表(实际上是一个可迭代的,比list更通用)。

But:但:

array[1:3] += [100, 100]

Works and adds (actually appends) the two elements in the middle (index 3 ) of array according to indexes:根据索引工作并添加(实际上是追加) array中间(索引3 )的两个元素:

[3, 2, 3, 100, 100, 4, 0]

Without using a for loop, as requested根据要求不使用for循环

If you want to add to the values in the slice:如果要添加到切片中的值:

array = [1,2,3,4,0]

array.__setitem__(slice(1, 3), [x+2 for x in array[1:3]])

# [1, 102, 103, 4, 0]
print(array)

Which can be written also as:也可以写成:

array = [1,2,3,4,0]

def apply_on_slice(lst, start, end, callable):
    array.__setitem__(slice(start, end), [callable(x) for x in array[start:end]])

apply_on_slice(array, 1, 3, lambda x : x + 100)

# [1, 102, 103, 4, 0]
print(array)

Using a for loop使用 for 循环

Here are some other options to do so, elegantly:这里有一些其他选项可以优雅地这样做:

array[1:3] = (x+2 for x in array[1:3])

Or of course, using a regular for loop, which is more efficient than using slicing twice:或者当然,使用常规的 for 循环,这比使用切片两次更有效:

for i in range(1, 3): 
    array[i] += 2

You are clearly expecting the operation to be applied element-wise, as in R and other languages (and within Python, in numpy arrays and the like).您显然希望该操作按元素应用,就像在 R 和其他语言中一样(以及在 Python 中,在numpy数组等中)。 Eg, adding 2 to a list would add 2 to each of the list's elements.例如,向列表中添加 2 将向列表的每个元素添加 2。 This is not how Python lists work: Each of the statements you ask about constructs one object on each side of the operator (a list or list slice, a list element, an integer), then applies the operation (just once) to these two objects.这不是 Python 列表的工作方式:您询问的每个语句都在运算符的每一侧构造一个对象(列表或列表切片、列表元素、整数),然后将操作(仅一次)应用于这两个对象对象。 So if you "add" two lists you get concatenation, if you try to add a list and an int you get a TypeError, etc. The details you can read in @Aviv's answer.因此,如果你“添加”两个列表,你会得到连接,如果你尝试添加一个列表和一个 int,你会得到一个 TypeError 等等。你可以在@Aviv 的答案中阅读详细信息。

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

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