Python:A[1:] 中的 x 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27652686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: What does for x in A[1:] mean?
提问by Margo Eastham
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]
,所以我认为for x in A[1:]:
相当于for(int i = 1; i < A.length; i++)
在 Java 中
But, after changing for x in A[1:]:
to for x in range(1,len(A))
, I got the wrong result
但是,更改for x in A[1:]:
为 后for x in range(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?
对不起,如果这是一个愚蠢的问题,但我不知道在哪里可以找到答案。有人能告诉我这个语法是做什么的,它叫什么吗?另外,你能给我相当于for x in A[1:]:
Java中的吗?
采纳答案by Preet Kukreti
This is array slicesyntax. See this SO question: Explain Python's slice notation.
这是数组切片语法。请参阅此 SO 问题: 解释 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-indexed 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的版本,您可以使用enumerate
:
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 的索引的实现细节,因此更有可能与支持切片语法的其他可迭代类型一起使用.
回答by Ignacio Vazquez-Abrams
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
, and 4
,而不是0
, 1
, and 2
。
回答by Abdelouahab
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]
而不是reversed(A)
反转列表。
回答by Saurabh Lende
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.
我想你可以通过这个例子了解更多。