如何获取列表的最后一个索引?
假设我有以下列表:
list1 = [1, 2, 33, 51]
^
|
indices 0 1 2 3
如何获取该列表的最后一个索引(在本例中为 3)?
Suppose I've the following list:
list1 = [1, 2, 33, 51]
^
|
indices 0 1 2 3
How do I obtain the last index, which in this case would be 3, of that list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
len(list1)-1
绝对是可行的方法,但是如果您绝对需要一个具有返回最后一个索引的函数的list
,您可以创建一个类继承自list
。len(list1)-1
is definitely the way to go, but if you absolutely need alist
that has a function that returns the last index, you could create a class that inherits fromlist
.获取列表最后一个索引的内容的最佳且快速的方法是使用
-1
作为索引号,例如:
输出为:
'hi'
。索引
-1
显示末尾的最后一个索引或第一个索引。但如果你只想获取最后一个索引,你可以用这个函数获取它:
在这种情况下,输入是列表,输出将是一个整数,即最后一个索引号。
The best and fast way to obtain the content of the last index of a list is using
-1
for number of index ,for example:
Output is:
'hi'
.Index
-1
shows you the last index or first index of the end.But if you want to get only the last index, you can obtain it with this function:
In this case, the input is the list, and the output will be an integer which is the last index number.
您的意思是
len(list1)-1
吗?如果您正在寻找其他方法,可以尝试
list1.index(list1[-1])
,但我不推荐这种方法。您必须确保该列表不包含重复项。Did you mean
len(list1)-1
?If you're searching for other method, you can try
list1.index(list1[-1])
, but I don't recommend this one. You will have to be sure, that the list contains NO duplicates.我猜你想要
将 3 存储在
last_index
中。I guess you want
which would store 3 in
last_index
.以上所有答案都是正确的,但
更准确地说,
因为数组以 0 开头
all above answers is correct but however
to be more precisely
since arrays is starting with 0
您可以使用列表长度。最后一个索引将是列表的长度减一。
You can use the list length. The last index will be the length of the list minus one.
感谢您的讨论。这让我更深入地研究了一些假设,并且有几个值得注意的不对称性:
我正在调试一个函数,该函数扫描数据点列表以查找分组并拒绝偶尔的故障。该函数使用 numpy 窗口中值滤波器来查找 2/3 和 1/3 转变点作为索引,然后返回它们。根据调用函数后索引的使用方式,可能会产生 1 的轻微差异。如果返回的索引用于对数据进行子集化,那么最后一个应该是:
在我看来,返回子集列表会更好,因为没有任何回旋余地可以错过最后一个元素。
最初,我正在修复一个条件,即数据结束时没有降至 1/3 以下,并且第一个索引用作最后一个索引。当我仔细查看解决方案时,我意识到最后一个索引总是缺少最后一个元素,因为它返回了索引,而不是过去的 1。
Thanks for the discussion. This made me look deeper at a few assumptions and a couple of asymmetries are worth noting:
I was debugging a function that scanned a list of data points looking for a grouping and rejecting occasional glitches. The function used a numpy windowed median filter to find the 2/3 and 1/3 transition points as indices and then it returned them. Depending on how the indices are used after the function is called could make a slight difference of 1. If the returned indeces are used to subset the data, then the last one should be:
In my gut, returning the subset list would be better because there would be no wiggle room to miss the last element.
Originally, I was fixing a condition where the data ended without dropping below the 1/3 level and the first index was used as the last index. When I looked closer at the solution, I realized that the last index was always missing the last element because it returned the index to it instead of 1 past it.
这可能是更Pythonic的方式:
list1.index(list1[-1])
This might be more pythonic way:
list1.index(list1[-1])
将返回列表的最后一个索引。
如果在数组索引之前使用减号,它将从末尾开始向下计数。 list1[-2] 将返回倒数第二个索引等。
值得一提的是,-0 仅返回列表的“第一个”(第 0 个)索引,因为 -0 和 0 是相同的数字,
will return the last index of your list.
If you use minus before the array index it will start counting downwards from the end. list1[-2] would return the second to last index etc.
Important to mention that -0 just returns the "first" (0th) index of the list because -0 and 0 are the same number,