Python: What does x mean in [1:]?

I tried to understand the Kadane algorithm from Wikipedia when I found this:

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 am not familiar with Python. I tried to understand what this syntax does, but I could not find the right answer because I did not know what he called. But I figured that A[1:] is the equivalent of skipping A[0] , so I thought that for x in A[1:]: equivalent to for(int i = 1; i < A.length; i++) in Java

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

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

+5
source share
4 answers

This is the syntax to slice an array . See this question: Explain Python fragment notation .

A list of my_list objects, for example. [1, 2, "foo", "bar"] , my_list[1:] equivalent to an incomplete copied list of all elements, starting from 0-indexed 1 : [2, "foo", "bar"] . So your for statement iterates over these objects:

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

range(..) returns a list / index generator (integers), so your operator for is to sort integers [1, 2, ..., len(my_list)]

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

So, in this latest version, you can use x as an index in the list: iter_obj = my_list[x] .

Alternatively, a slightly larger pythonic version, if you still need an iteration index (for example, to โ€œcountโ€ the current object), you can use 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 promising evidence if you decide to change the my_list type to something else, since it does not rely on the details of the implementation of indexing based on 0 and, therefore, is more likely to work with other iterative types that support slice syntax.

+6
source

Here are some of the examples I 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 with these examples.

+9
source

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

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

This is used to trim your list from the first item.

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

+1
source

Source: https://habr.com/ru/post/1209902/


All Articles