识别列表中项目的最后一次出现 [英] Indentifying the LAST occurrence of an item in a list
问题描述
对于任何列表x,x.index(item)返回x中项目的第一个
出现的索引。有没有一种简单的方法来识别列表中项目的最后一次出现?我的解决方案感觉很复杂 -
反转列表,在
反向列表中查找项目的第一次出现,然后从列表长度中减去其索引
- 1,即
LastOcc = len(x) - 1 - x [:: - 1] .index(item)
有更简单的解决方案吗?
提前致谢
Thomas Philips
For any list x, x.index(item) returns the index of the FIRST
occurrence of the item in x. Is there a simple way to identify the
LAST occurrence of an item in a list? My solution feels complex -
reverse the list, look for the first occurence of the item in the
reversed list, and then subtract its index from the length of the list
- 1, i.e.
LastOcc = len(x) - 1 - x[::-1].index(item)
Is there a simpler solution?
Thanks in advance
Thomas Philips
推荐答案
< tk **** @ hotmail.comwrote in message
news:11 ** ********************@w1g2000hsg.googlegro ups.com ...
|对于任何列表x,x.index(item)返回FIRST的索引
|在x中出现该项目。有没有一种简单的方法来识别
|列表中最后一次出现的项目?我的解决方案感觉很复杂 -
|反转列表,在
|中查找项目的第一次出现反转列表,然后从列表长度中减去其索引
| - 1,即
|
| LastOcc = len(x) - 1 - x [:: - 1] .index(item)
|
|是否有一个更简单的解决方案?
除非我希望列表颠倒,否则我会简单地向后迭代,
在实用程序函数中是不可能的。
def rindex(lis,item):
for i in range(len(lis)-1,-1,-1):
如果item == lis [i]:
返回i
else:
引发ValueError(" rindex(lis,item):item不是在lis)
t = [0,1,2,3,0]
打印rindex(t,3)
打印rindex(t,0)
打印rindex(t,4)
#打印3,4和追溯
Terry Jan Reedy
<tk****@hotmail.comwrote in message
news:11**********************@w1g2000hsg.googlegro ups.com...
| For any list x, x.index(item) returns the index of the FIRST
| occurrence of the item in x. Is there a simple way to identify the
| LAST occurrence of an item in a list? My solution feels complex -
| reverse the list, look for the first occurence of the item in the
| reversed list, and then subtract its index from the length of the list
| - 1, i.e.
|
| LastOcc = len(x) - 1 - x[::-1].index(item)
|
| Is there a simpler solution?
Unless I wanted the list reversed, I would simply iterate backwards,
parhaps in a utility function.
def rindex(lis, item):
for i in range(len(lis)-1, -1, -1):
if item == lis[i]:
return i
else:
raise ValueError("rindex(lis, item): item not in lis")
t=[0,1,2,3,0]
print rindex(t,3)
print rindex(t,0)
print rindex(t,4)
# prints 3, 4, and traceback
Terry Jan Reedy
4月4日上午10:55,Terry Reedy < tjre ... @ udel.eduwrote:
On Apr 4, 10:55 am, "Terry Reedy" <tjre...@udel.eduwrote:
< tkp ... @ hotmail.comwrote in message
新闻:11 ********************** @ w1g2000hsg.googlegro ups.com ...
|对于任何列表x,x.index(item)返回FIRST的索引
|在x中出现该项目。有没有一种简单的方法来识别
|列表中最后一次出现的项目?我的解决方案感觉很复杂 -
|反转列表,在
|中查找项目的第一次出现反转列表,然后从列表长度中减去其索引
| - 1,即
|
| LastOcc = len(x) - 1 - x [:: - 1] .index(item)
|
|有没有更简单的解决方案?
<tkp...@hotmail.comwrote in message
news:11**********************@w1g2000hsg.googlegro ups.com...
| For any list x, x.index(item) returns the index of the FIRST
| occurrence of the item in x. Is there a simple way to identify the
| LAST occurrence of an item in a list? My solution feels complex -
| reverse the list, look for the first occurence of the item in the
| reversed list, and then subtract its index from the length of the list
| - 1, i.e.
|
| LastOcc = len(x) - 1 - x[::-1].index(item)
|
| Is there a simpler solution?
如何 :
l = [1,2, 1,3,1,5]
target = 1
为索引,val为枚举(l):
如果val == 1 :
lastIndexOf = index
print lastIndexOf
How about:
l = [1, 2, 1, 3, 1, 5]
target = 1
for index, val in enumerate(l):
if val==1:
lastIndexOf = index
print lastIndexOf
4月4日,11日:上午20点,7stud < bbxx789_0 ... @ yahoo.comwrote:
On Apr 4, 11:20 am, "7stud" <bbxx789_0...@yahoo.comwrote:
4月4日上午10:55,Terry Reedy < tjre ... @ udel.eduwrote:
On Apr 4, 10:55 am, "Terry Reedy" <tjre...@udel.eduwrote:
< tkp ... @ hotmail.comwrote in message
<tkp...@hotmail.comwrote in message
新闻:11 ********************** @ w1g2000hsg.googlegro ups.com ...
|对于任何列表x,x.index(item)返回FIRST的索引
|在x中出现该项目。有没有一种简单的方法来识别
|列表中最后一次出现的项目?我的解决方案感觉很复杂 -
|反转列表,在
|中查找项目的第一次出现反转列表,然后从列表长度中减去其索引
| - 1,即
|
| LastOcc = len(x) - 1 - x [:: - 1] .index(item)
|
|有更简单的解决方案吗?
news:11**********************@w1g2000hsg.googlegro ups.com...
| For any list x, x.index(item) returns the index of the FIRST
| occurrence of the item in x. Is there a simple way to identify the
| LAST occurrence of an item in a list? My solution feels complex -
| reverse the list, look for the first occurence of the item in the
| reversed list, and then subtract its index from the length of the list
| - 1, i.e.
|
| LastOcc = len(x) - 1 - x[::-1].index(item)
|
| Is there a simpler solution?
怎么样 :
l = [1,2,1,3,1, 5]
target = 1
表示索引,val表示枚举(l):
如果val == 1:
lastIndexOf = index
打印lastIndexOf
How about:
l = [1, 2, 1, 3, 1, 5]
target = 1
for index, val in enumerate(l):
if val==1:
lastIndexOf = index
print lastIndexOf
Nahh。可怕的解决方案。你应该从列表的最后开始。
Nahh. Horrible solution. You should start at the end of the list.
这篇关于识别列表中项目的最后一次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!