[英]How to find the index of the last occurrence of nested list that contains a specific element?
I need to (eventually) insert a value in a specific place in a nested list, but it needs to be inserted after the last occurrence of a specific string in the nested list...So I need to first find the index of this last occurrence.我需要(最终)在嵌套列表中的特定位置插入一个值,但需要在嵌套列表中最后一次出现特定字符串之后插入它......所以我需要首先找到最后一个的索引发生。 Might make more sense with an example (I will generalize):举个例子可能更有意义(我会概括):
a = 'RPGAccount'
input = [['RPGAccount', 'ddi=0', 'pp=0', 'kol=0', 'sddf=1234567890', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n'], ['RPGAccount', 'ddi=0', 'pp=0', 'kol=0', 'sddf=NA', '4233f2dsfa=abc', 'igg=source', 'id=4g43g34b433435n35n'], ['RPGAdditional', 'addkey=asdf', 'addvalue=false', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n', 'tz=asdf'], ['RPGAdditional', 'addkey=device_id', 'addvalue=F309387C-AAF5-478D-95A2-28E9B46105C7', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n', 'tz=asdf'], ['RPGAdditional', 'addkey=sdaf', 'addvalue=fixed_ap', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n', 'tz=asdf’]]
b = 1
So I need to have an index of 1 returned, since 1 is the index of the list所以我需要返回 1 的索引,因为 1 是列表的索引
['RPGAccount', 'ddi=0', 'pp=0', 'kol=0', 'sddf=NA', '4233f2dsfa=abc', 'igg=source', 'id=4g43g34b433435n35n']
which contains the last occurrence of the string 'RPGAccount'其中包含最后一次出现的字符串“RPGAccount”
I know I should be doing something like:我知道我应该做这样的事情:
for l in input:
for l_ in l:
if a in l_:
print input.index[l]
else:
pass
Solution:解决方案:
some_list = []
for l in input:
for l_ in l:
if a in l_:
some_list.append(input.index(l))
else:
pass
location_ = some_list[-1]
print location_
You can do that like this:你可以这样做:
a = 'RPGAccount'
input = [['RPGAccount', 'ddi=0', 'pp=0', 'kol=0', 'sddf=1234567890', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n'],
['RPGAccount', 'ddi=0', 'pp=0', 'kol=0', 'sddf=NA', '4233f2dsfa=abc', 'igg=source', 'id=4g43g34b433435n35n'],
['RPGAdditional', 'addkey=asdf', 'addvalue=false', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n', 'tz=asdf'],
['RPGAdditional', 'addkey=device_id', 'addvalue=F309387C-AAF5-478D-95A2-28E9B46105C7', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n', 'tz=asdf'],
['RPGAdditional', 'addkey=sdaf', 'addvalue=fixed_ap', '4233f2dsfa=abc', 'igg=dev1', 'id=4g43g34b433435n35n', 'tz=asdf']]
# Index of last sublist containing a - if not found returns -1
b = next((len(input) - i - 1 for i, lst in enumerate(reversed(input)) if a in lst), -1)
print(b)
# 1
Using your loop, it would be more efficient to do as follows:使用您的循环,执行以下操作会更有效:
location_ = -1
for i, l in enumerate(input):
for l_ in l:
if a in l_:
location_ = i
print location_
# 1
Sorry, mistake was small and I resolved it...对不起,错误很小,我解决了它......
some_list = []
for l in input:
for l_ in l:
if a in l_:
some_list.append(input.index(l))
else:
pass
location_ = some_list[-1]
print location_
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.