![]() ![]() This way, the slice starts at the penultimate element, a (since a does not include a) and goes backwards until the element "before" a, meaning that a is included in the slice. i and j are equal – you start and stop slicing in the same place – so the expression evaluates to an empty list. Therefore, a means "go from the end of a, which is a (since i is unspecified and k is negative), to the last element of a (since j = -1), with step size of -1". To get the desired behaviour, you must use -len(a)-1 (or -(len(a)+1)) in place of j, which means that to get to a, slice starts at the last element of a, goes left for len(a) elements and then left one more element, ending up before a starts and thus including a in the slice. NB: j cannot be replaced with -1, since doing that will cause Python to treat j as the last element of a rather than the (nonexistent) element before a. If k is negative, you're slicing backwards, so i becomes len(a) and j becomes the element before the start of a. if k is positive, you're slicing forwards, so i becomes 0 and j becomes len(a).If i or j are not specified, or are None, they default to the ends of a, but which ends depends on the sign of k: ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |