Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
last index
#1
Hi

In Python, starting indexes from 0 (it has been the choice of Guido Van Rossum from what I read some time ago, but it's confusing compare to Matlab or Julia for example) and it leads to some quirkinesses, but maybe I'm doing wrong and that's why more experienced people may give me/us a more relevant method.

Some practical examples and my understanding:
## array copy
B1 = np.copy(A[3:, :]);      # here all the lines fron the 3rd up the last one are taken into account
B2 = np.copy(A[3:-1, :]);    # as usually in python the last index is not included in the list

## now the goal is to delete the last column/row => "-1" is used
C = np.delete(A, -1, axis=1); # the last column is removed
D = np.delete(A, -1, axis=0); # ditto for the last row
Is there another (relevant) way to proceed?
(of course I did some searches on internet, but that's the only I've found so far)

Thanks for your feedback

Paul
Reply
#2
Think of the indexes as being between the items, rather than on the items:

Output:
lunch = ['Spam', 'spam', 'spam', 'eggs', 'spam'] ^ ^ ^ ^ ^ ^ | | | | | | 0 1 2 3 4 5 -5 -4 -3 -2 -1
If you just give one index, you get the item after it. That's why -1 gets you the last item. But if you give two indexes, you get everything between them. That's why 1:-1 gets you everything but the first and last item.

BTW, Guido didn't come up with the idea. It's been around since the sixties, at least in computer science. There's more about the reasoning at Wikipedia.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
thanks for the feedback and the additional information's;

I cannot tell about the potential advantages of starting with 0, but it's sometime confusing (probably a question of pratice).
(on that specific topic, other languages are more "natural" in my opinion)

In any way thanks

Paul
Reply
#4
(Jul-31-2019, 01:27 PM)paul18fr Wrote: I cannot tell about the potential advantages of starting with 0, but it's sometime confusing (probably a question of pratice).
(on that specific topic, other languages are more "natural" in my opinion)

Question: is your age confusing or less-natural? Because you are born 0 years old, not 1 year old. Are you confused while calculating your age because it's starts with 0 and not with 1? Probably not Smile

Some relevant pieces of information regarding zero based index:

Why Python uses 0-based indexing by Guido van Rossum

Dijkstra's legendary Why numbering should start at zero (handwritten version)

and finally real reason why 0-based indexing is used:

Quote:So: the technical reason we started counting arrays at zero is that in the mid-1960’s, you could shave a few cycles off of a program’s compilation time on an IBM 7094. The social reason is that we had to save every cycle we could, because if the job didn’t finish fast it might not finish at all and you never know when you’re getting bumped off the hardware because the President of IBM just called and fuck your thesis, it’s yacht-racing time.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
in linear algebra or even in mechanical enginnering, the first cell is (1,1), not (0,0) and it has been chosen by another languages (Matlab, Julia, Fortan and so on).

But as I said, it's a question of pratice, and my goal is not to troll

Paul
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Getting Index Error - list index out of range krishna 2 1,024 Jan-09-2021, 08:29 AM
Last Post: buran
  Getting Index Error - list index out of range RahulSingh 2 3,374 Feb-03-2020, 07:17 AM
Last Post: RahulSingh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020