0

For a Python substring, I'm confused on defaults of x,y when z is negative

Next lines: the defaults for x:y are no problem understanding because z is positive

alpha = "abcdef"
print(alpha[0:6:1])  #abcdef
print(alpha[::1])      #abcdef    defaults for x,y change this to [0:6:1]

But when change to a negative z I'm confused. I would think [::z] becomes [0:6:z] regardless of z being +1 or -1. But seems defaults for x,y are different when the z is pos or neg

print(alpha[0:6:-1])   # (no defaults example) prints nothing - OK
print(alpha[::1])      # abcdef    default [::1] becomes [0:6:1] - OK
print(alpha[::-1])     # fedcba    default [::-1] not becomes [0:6:-1] - why different x,y defaults?

Response to suggested duplicate

I don't think this is a duplicate - the other question is about negative x,y. This questions is about x,y defaults when z is negative. This question involves a double colon syntax - that does not appear in the "duplicate question" reference.

halfer
  • 19,824
  • 17
  • 99
  • 186
Playing with GAS
  • 565
  • 3
  • 10
  • 18
  • Ya, that was a bad dupe closure (wasn't me!). I've nominated it for reopening, although I believe I recall another dupe that does answer this. – Carcigenicate Jan 10 '21 at 20:08
  • I believe the "why" though is simply convenience. If the defaults didn't swap, it would be necessary to specify them every time a negative step is used, which means `coll[::-1]` would never be correct, and would need to be `coll[len(coll):-1:-1]`, which is ugly af. It makes sense if you think of the step as the direction you travel in. There are no elements when you travel backwards from index 0. – Carcigenicate Jan 10 '21 at 20:10
  • @Carcigenicate I believe this is an appropriate target https://stackoverflow.com/questions/63766793 – cigien Jan 10 '21 at 22:57
  • @cigien Their questions appears to not be what a negative step is, but why a negative step reverses the default values of the other two "arguments" of the slice. None of those answers seem to address that. – Carcigenicate Jan 10 '21 at 22:59
  • @Carcigenicate Hmm, that seems to be covered under "I don't understand how this works", but you might be right. Note however that deciding whether a question is a duplicate doesn't really have anything to do with the *answers* on the proposed target. If the answers don't cover some aspect of the question, then an additional answer should be added on the target. – cigien Jan 10 '21 at 23:10
  • Thanks, carcigenicate, for re-open – Playing with GAS Jan 12 '21 at 15:44
  • Here is my conclusion. – Playing with GAS Jan 12 '21 at 16:37
  • Tests for rules: (sorry, I don't see how to designate code in a comment) alpha = "abcdef" print("2 "+alpha[6:0:-1]) # fedcba no defaults, -z: defaults are normal print("3 "+alpha[0:6:-1]) # nothing no defaults, -z: defaults are normal print("4 "+alpha[6::-1]) # fedcba x explicit, y default=len+1, -z. w/ one default, defaults are normal print("5 "+alpha[:6:-1]) # nothing default x=0, explicit y, -z. rule: w/ one default, defaults are normal print("6 "+alpha[::-1]) # fedcba *if default x&y AND -z*: Defaults changed to x=len, y= "0-1"(so 0 included) – Playing with GAS Jan 12 '21 at 16:41
  • Rules: 1 - Order of evaluation will always be x to y. 2 - Defaults are x=0, y=len+1, z = +1 2B - *Default exception* if x,y both defaults AND -z then defaults change to: x = len+1, y="0-1" (so include 0) – Playing with GAS Jan 12 '21 at 16:43

0 Answers0