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
這是數組切片語法。看到這個問題:解釋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)扭轉名單。