如何获取列表的最后一个索引?

发布于 2024-12-12 03:02:44 字数 164 浏览 0 评论 0原文

假设我有以下列表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

心安伴我暖 2024-12-19 03:02:44

len(list1)-1 绝对是可行的方法,但是如果您绝对需要一个具有返回最后一个索引的函数的 list,您可以创建一个类继承自list

class MyList(list):
    def last_index(self):
        return len(self)-1


>>> l=MyList([1, 2, 33, 51])
>>> l.last_index()
3

len(list1)-1 is definitely the way to go, but if you absolutely need a list that has a function that returns the last index, you could create a class that inherits from list.

class MyList(list):
    def last_index(self):
        return len(self)-1


>>> l=MyList([1, 2, 33, 51])
>>> l.last_index()
3
荒岛晴空 2024-12-19 03:02:44

获取列表最后一个索引的内容的最佳且快速的方法是使用 -1 作为索引号,
例如:

my_list = [0, 1, 'test', 2, 'hi']
print(my_list[-1])

输出为:'hi'

索引 -1 显示末尾的最后一个索引或第一个索引。

但如果你只想获取最后一个索引,你可以用这个函数获取它:

def last_index(input_list:list) -> int:
    return len(input_list) - 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:

my_list = [0, 1, 'test', 2, 'hi']
print(my_list[-1])

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:

def last_index(input_list:list) -> int:
    return len(input_list) - 1

In this case, the input is the list, and the output will be an integer which is the last index number.

海螺姑娘 2024-12-19 03:02:44

您的意思是 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.

始终不够爱げ你 2024-12-19 03:02:44

我猜你想要

last_index = len(list1) - 1 

将 3 存储在 last_index 中。

I guess you want

last_index = len(list1) - 1 

which would store 3 in last_index.

蝶舞 2024-12-19 03:02:44

以上所有答案都是正确的,但

a = [];
len(list1) - 1 # where 0 - 1 = -1

更准确地说,

a = [];
index = len(a) - 1 if a else None;

if index == None : raise Exception("Empty Array")

因为数组以 0 开头

all above answers is correct but however

a = [];
len(list1) - 1 # where 0 - 1 = -1

to be more precisely

a = [];
index = len(a) - 1 if a else None;

if index == None : raise Exception("Empty Array")

since arrays is starting with 0

不再让梦枯萎 2024-12-19 03:02:44

您可以使用列表长度。最后一个索引将是列表的长度减一。

len(list1)-1 == 3

You can use the list length. The last index will be the length of the list minus one.

len(list1)-1 == 3
花想c 2024-12-19 03:02:44
a = ['1', '2', '3', '4']
print len(a) - 1
3
a = ['1', '2', '3', '4']
print len(a) - 1
3
眉目亦如画i 2024-12-19 03:02:44

感谢您的讨论。这让我更深入地研究了一些假设,并且有几个值得注意的不对称性:

>>> l = [1,2,3,4]
>>> l
[1, 2, 3, 4]
>>> l[0:-1]
[1, 2, 3]
>>> l[0:3]
[1, 2, 3]
>>> l[0:4]
[1, 2, 3, 4]
>>> l[0:1000]
[1, 2, 3, 4]
>>> l[-1:]
[4]
  1. 在索引列表时,第一个索引返回第一个元素,它也可用于将其包含在切片中。而最后一个索引则不能。
  2. 用于切片的最后一个索引可以是比最后一个元素的索引大的任何数字,但第一个索引只能是 0。

我正在调试一个函数,该函数扫描数据点列表以查找分组并拒绝偶尔的故障。该函数使用 numpy 窗口中值滤波器来查找 2/3 和 1/3 转变点作为索引,然后返回它们。根据调用函数后索引的使用方式,可能会产生 1 的轻微差异。如果返回的索引用于对数据进行子集化,那么最后一个应该是:

  len(data)
instead of:
  len(data) - 1
 or:
  -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:

>>> l = [1,2,3,4]
>>> l
[1, 2, 3, 4]
>>> l[0:-1]
[1, 2, 3]
>>> l[0:3]
[1, 2, 3]
>>> l[0:4]
[1, 2, 3, 4]
>>> l[0:1000]
[1, 2, 3, 4]
>>> l[-1:]
[4]
  1. In indexing a list, the first index returns the first element, it also can be used to include it in a slice. Whereas the last index cannot.
  2. The last index for slicing can be any number larger than the index of the last element but the first index can only be 0.

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:

  len(data)
instead of:
  len(data) - 1
 or:
  -1

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.

暮凉 2024-12-19 03:02:44

这可能是更Pythonic的方式:

list1.index(list1[-1])

This might be more pythonic way:

list1.index(list1[-1])

魔法少女 2024-12-19 03:02:44
list1[-1]

将返回列表的最后一个索引。

如果在数组索引之前使用减号,它将从末尾开始向下计数。 list1[-2] 将返回倒数第二个索引等。

值得一提的是,-0 仅返回列表的“第一个”(第 0 个)索引,因为 -0 和 0 是相同的数字,

list1[-1]

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文