last day (15 days later) » 

00:27
98
A: Why does Python start at index 1 when iterating an array backwards?

recnacTo explain it in another way, because -0 is equal to 0, if backward starts from 0, it is ambiguous to the interpreter. If you are confused about -, and looking for another way to index backwards more understandably, you can try ~, it is a mirror of forward: arr = ["a", "b", "c", "d"] print(a...

Thanks to all for answer. I wish python has another mechanism to get list backward. I would like to hear if already exist.
I have update another way by ~, maybe it is what you are looking for @abraham
Your update is now more helpful. Bunch of thanks
you are welcome, ~ is very useful to swap mirror value or get median number @abraham
Wow. learnt a new trick to do reverse indexing using ~. Thanks mate.
00:27
glad to hear that, actually it is a math trick of inverse code and complement code, and ~ is indeed more easy-understaning in some situation : ) @SukumarRdjf
you can see the comment, he want another way more understandable instead of -, so I add this : ) @NicolasB
@SukumarRdjf Please don't ever use this "trick".
@gerrit I don't know… it's easier to read than -1-x.
Voo
Voo
@wizzwizz4 Is it? -x - 1 can be understood without knowing how two's complement numbers are derived, while the latter requires not only knowing these details but also that Python guarantees the format.
In my opinion, if it is a code maintained by yourself, you can use any trick to avoid some bug, because of maybe a high readability and usability. But in team work, if your co-workers not familiar with it, avoid using 'too clever' code, may bring troubles to others. @Voo
I meant why is it so? shouldn't it be 1 as complement?
00:27
About teamwork - your "past you" and your "future you" are always your teammates. Your "past you" didn't know about this trick whey them wrote their code, your "present you" knows this trick and thinks it is okay, and your "future you" will absolutely hate you when he has to give maintenance to this code two years down the line and have already forgetted what the hell you were doing there. Just a tip.
@T.Sar your theory sounds make sense : )
actually ~0 is the inverse code of 0 @Vicrobot
what did you mean by inverse code?
@Voo The expression -x-1 can be understood, granted, that is, which number it evaluates to. But the issue is which element of arr arr[-x-1] means. This entails knowing whether or not Python has an out-by-one error here. And in this respect arr[-x] & arr[-x-1] are no better than arr[~x].
Voo
Voo
@Rosie In one case you have to make the assumption that -0 = 0 in the other you have to rely on a very specific format on how to encode numbers. That does strike me as rather different. Personally I like languages that make the distinction explicit since it not only simplifies the math when computing bounds but also avoids the ambiguity.
To everyone complaining about this, is there a time it will fail? The -x convention works great, until you do some fancy computations to get x and the right combination gives x=0. Suddenly, future you is really annoyed at python's indexing. This seems to avoid that nicely.
00:27
@Vicrobot Python's invert operator ~ does 2's complement
I like the don't be too tricky discussion.
2

last day (15 days later) »