Python:A [1:]中x的含义是什么?

时间:2021-05-19 16:57:08

I was trying to understand Kadane's algorithm from Wikipedia, when I found this:

当我发现这个时,我试图从*中了解Kadane的算法:

def max_subarray(A):
    max_ending_here = max_so_far = A[0]
    for x in A[1:]:
        max_ending_here = max(x, max_ending_here + x)
        max_so_far = max(max_so_far, max_ending_here)
    return max_so_far

I'm not familiar with Python. I tried to google what this syntax does but I couldn't find the right answer because I didn't know what's it called. But, I figured A[1:] is the equivalent of omitting A[0], so I thought for x in A[1:]: is equivalent to for(int i = 1; i < A.length; i++) in Java

我不熟悉Python。我试着谷歌这个语法做了什么,但我找不到正确的答案因为我不知道它叫什么。但是,我认为A [1:]相当于省略A [0],所以我认为A中的x [1:]:相当于for(int i = 1; i ;>

But, after changing for x in A[1:]: to for x in range(1,len(A)), I got the wrong result

但是,在将A [1:]中的x更改为范围内的x(1,len(A))之后,我得到了错误的结果

Sorry if this is a stupid question, but I don't know where else to find the answer. Can somebody tell me what this syntax does and what is it called? Also, could you give me the equivalent of for x in A[1:]: in Java?

对不起,如果这是一个愚蠢的问题,但我不知道在哪里可以找到答案。有人能告诉我这个语法是做什么的,它叫做什么?另外,你能在Java中给我相当于A [1:]中的x吗?

4 个解决方案

#1


6  

This is array slice syntax. See this SO question: Explain Python's slice notation .

这是数组切片语法。看到这个问题:解释Python的切片表示法。

For a list my_list of objects e.g. [1, 2, "foo", "bar"], my_list[1:] is equivalent to a shallow copied list of all elements starting from the 0-indexed 1: [2, "foo", "bar"]. So your for statement iterates over these objects:

对于列表my_list对象,例如[1,2,“foo”,“bar”],my_list [1:]相当于从0索引1开始的所有元素的浅复制列表:[2,“foo”,“bar”]。所以你的for语句迭代这些对象:

for-iteration 0: x == 2 
for-iteration 1: x == "foo" 
for-iteration 2: x == "bar" 

range(..) returns a list/generator of indices (integers), so your for statement would iterate over integers [1, 2, ..., len(my_list)]

range(..)返回索引(整数)的列表/生成器,因此你的for语句将迭代整数[1,2,...,len(my_list)]

for-iteration 0: x == 1 
for-iteration 1: x == 2
for-iteration 2: x == 3

So in this latter version you could use x as an index into the list: iter_obj = my_list[x].

所以在后一个版本中你可以使用x作为列表的索引:iter_obj = my_list [x]。

Alternatively, a slightly more pythonic version if you still need the iteration index (e.g. for the "count" of the current object), you could use enumerate:

或者,如果仍然需要迭代索引(例如当前对象的“计数”),则可以使用枚举:更多pythonic版本:

for (i, x) in enumerate(my_list[1:]):
    # i is the 0-based index into the truncated list [0, 1, 2]
    # x is the current object from the truncated list [2, "foo", "bar"]

This version is a bit more future proof if you decide to change the type of my_list to something else, in that it does not rely on implementation detail of 0-based indexing, and is therefore more likely to work with other iterable types that support slice syntax.

如果您决定将my_list的类型更改为其他内容,则此版本将更具未来性,因为它不依赖于基于0的索引的实现细节,因此更有可能与支持切片的其他可迭代类型一起使用句法。

#2


11  

Here are some of the example that I have tried

以下是我尝试过的一些示例

>>> a=[1,5,9,11,2,66]

>>> a[1:]
[5, 9, 11, 2, 66]

>>> a[:1]
[1]

>>> a[-1:]
[66]

>>> a[:-1]
[1, 5, 9, 11, 2]

>>> a[3]
11

>>> a[3:]
[11, 2, 66]

>>> a[:3]
[1, 5, 9]

>>> a[-3:]
[11, 2, 66]

>>> a[:-3]
[1, 5, 9]

>>> a[::1]
[1, 5, 9, 11, 2, 66]

>>> a[::-1]
[66, 2, 11, 9, 5, 1]

>>> a[1::]
[5, 9, 11, 2, 66]

>>> a[::-1]
[66, 2, 11, 9, 5, 1]

>>> a[::-2]
[66, 11, 5]

>>> a[2::]
[9, 11, 2, 66]

I think you can understand more by this examples.

我想你可以通过这个例子了解更多。

#3


2  

Unlike other languages, iterating over a sequence in Python yields the elements within the sequence itself. This means that iterating over [1, 2, 4] yields 1, 2, and 4 in turn, and not 0, 1, and 2.

与其他语言不同,迭代Python中的序列会产生序列本身的元素。这意味着迭代[1,2,4]依次产生1,2和4,而不是0,1和2。

#4


1  

A = [1, 2, 3]

A[1:] == [2, 3]

This is used to truncate your list from the first element.

这用于从第一个元素截断列表。

And note that lists are mutable, if you find something like A[:] that means, they want to create a double of this list, without altering the original list, and use A[::-1] instead of reversed(A) to reverse the list.

并注意列表是可变的,如果你发现类似A [:]这意味着,他们想要创建这个列表的两倍,而不改变原始列表,并使用A [:: - 1]而不是反转(A)扭转名单。

#1


6  

This is array slice syntax. See this SO question: Explain Python's slice notation .

这是数组切片语法。看到这个问题:解释Python的切片表示法。

For a list my_list of objects e.g. [1, 2, "foo", "bar"], my_list[1:] is equivalent to a shallow copied list of all elements starting from the 0-indexed 1: [2, "foo", "bar"]. So your for statement iterates over these objects:

对于列表my_list对象,例如[1,2,“foo”,“bar”],my_list [1:]相当于从0索引1开始的所有元素的浅复制列表:[2,“foo”,“bar”]。所以你的for语句迭代这些对象:

for-iteration 0: x == 2 
for-iteration 1: x == "foo" 
for-iteration 2: x == "bar" 

range(..) returns a list/generator of indices (integers), so your for statement would iterate over integers [1, 2, ..., len(my_list)]

range(..)返回索引(整数)的列表/生成器,因此你的for语句将迭代整数[1,2,...,len(my_list)]

for-iteration 0: x == 1 
for-iteration 1: x == 2
for-iteration 2: x == 3

So in this latter version you could use x as an index into the list: iter_obj = my_list[x].

所以在后一个版本中你可以使用x作为列表的索引:iter_obj = my_list [x]。

Alternatively, a slightly more pythonic version if you still need the iteration index (e.g. for the "count" of the current object), you could use enumerate:

或者,如果仍然需要迭代索引(例如当前对象的“计数”),则可以使用枚举:更多pythonic版本:

for (i, x) in enumerate(my_list[1:]):
    # i is the 0-based index into the truncated list [0, 1, 2]
    # x is the current object from the truncated list [2, "foo", "bar"]

This version is a bit more future proof if you decide to change the type of my_list to something else, in that it does not rely on implementation detail of 0-based indexing, and is therefore more likely to work with other iterable types that support slice syntax.

如果您决定将my_list的类型更改为其他内容,则此版本将更具未来性,因为它不依赖于基于0的索引的实现细节,因此更有可能与支持切片的其他可迭代类型一起使用句法。

#2


11  

Here are some of the example that I have tried

以下是我尝试过的一些示例

>>> a=[1,5,9,11,2,66]

>>> a[1:]
[5, 9, 11, 2, 66]

>>> a[:1]
[1]

>>> a[-1:]
[66]

>>> a[:-1]
[1, 5, 9, 11, 2]

>>> a[3]
11

>>> a[3:]
[11, 2, 66]

>>> a[:3]
[1, 5, 9]

>>> a[-3:]
[11, 2, 66]

>>> a[:-3]
[1, 5, 9]

>>> a[::1]
[1, 5, 9, 11, 2, 66]

>>> a[::-1]
[66, 2, 11, 9, 5, 1]

>>> a[1::]
[5, 9, 11, 2, 66]

>>> a[::-1]
[66, 2, 11, 9, 5, 1]

>>> a[::-2]
[66, 11, 5]

>>> a[2::]
[9, 11, 2, 66]

I think you can understand more by this examples.

我想你可以通过这个例子了解更多。

#3


2  

Unlike other languages, iterating over a sequence in Python yields the elements within the sequence itself. This means that iterating over [1, 2, 4] yields 1, 2, and 4 in turn, and not 0, 1, and 2.

与其他语言不同,迭代Python中的序列会产生序列本身的元素。这意味着迭代[1,2,4]依次产生1,2和4,而不是0,1和2。

#4


1  

A = [1, 2, 3]

A[1:] == [2, 3]

This is used to truncate your list from the first element.

这用于从第一个元素截断列表。

And note that lists are mutable, if you find something like A[:] that means, they want to create a double of this list, without altering the original list, and use A[::-1] instead of reversed(A) to reverse the list.

并注意列表是可变的,如果你发现类似A [:]这意味着,他们想要创建这个列表的两倍,而不改变原始列表,并使用A [:: - 1]而不是反转(A)扭转名单。