简体   繁体   English

查找列表中两个标签之间的元素

[英]Find elements between two tags in a list

Language: Python 3.4 OS: Windows 8.1语言:Python 3.4 操作系统:Windows 8.1

I have some lists like the following:我有一些如下列表:

a = ['text1', 'text2', 'text3','text4','text5']
b = ['text1', 'text2', 'text3','text4','New_element', 'text5']

What is the simplest way to find the elements between two tags in a list?在列表中的两个标签之间查找元素的最简单方法是什么?

I want to be able to get it even if the lists and tags have variable number of elements or variable length.即使列表和标签具有可变数量的元素或可变长度,我也希望能够获得它。

Ex: get elements between text1 and text4 or text1 or text5, etc. Or get the elements between text1 and text5 that has longer length.例如:获取 text1 和 text4 或 text1 或 text5 之间的元素,等等。或者获取 text1 和 text5 之间长度更长的元素。

I tried using regular expressions like:我尝试使用正则表达式,例如:

re.findall(r'text1(.*?)text5', a)

This will give me an error I guess because you can only use this in a string but not lists.我猜这会给我一个错误,因为你只能在字符串中使用它,而不能在列表中使用它。

To get the location of an element in a list use index() .要获取列表中元素的位置,请使用index() Then use the discovered index to create a slice of the list like:然后使用发现的索引来创建列表的一部分,例如:

Code:代码:

print(b[b.index('text3')+1:b.index('text5')])

Results:结果:

['text4', 'New_element']

You can use the list.index method to find the first occurrence of each of your tags, then slice the list to get the value between the indexes.您可以使用list.index方法找到每个标签的第一次出现,然后对列表进行切片以获取索引之间的值。

def find_between_tags(lst, start_tag, end_tag):
    start_index = lst.index(start_tag)
    end_index = lst.index(end_tag, start_index)
    return lst[start_index + 1: end_index]

If either of the tags is not in the list (or if the end tag only occurs before the start tag), one of the index calls will raise a ValueError .如果任一标记不在列表中(或者如果结束标记仅出现在开始标记之前),其中一个index调用将引发ValueError You could suppress the exception if you want to do something else, but just letting the caller deal with it seems like a reasonable option to me, so I've left the exception uncaught.如果你想做其他事情,你可以抑制异常,但让调用者处理它对我来说似乎是一个合理的选择,所以我没有捕获异常。

If the tags might occur in this list multiple times, you could extend the logic of the function above to find all of them.如果标签可能多次出现在这个列表中,您可以扩展上面 function 的逻辑以找到所有标签。 For this you'll want to use the start argument to list.index , which will tell it not to look at values before the previous end tag.为此,您需要使用list.indexstart参数,这将告诉它不要查看前一个结束标记之前的值。

def find_all_between_tags(lst, start_tag, end_tag):
    search_from = 0
    try:
        while True:
            start_index = lst.index(start_tag, search_from)
            end_index = lst.index(end_tag, start_index + 1)
            yield lst[start_index + 1:end_index]
            search_from = end_index + 1
    except ValueError:
        pass

This generator does suppress the ValueError , since it keeps on searching until it can't find another pair of tags.这个生成器确实抑制了ValueError ,因为它一直在搜索,直到找不到另一对标签。 If the tags don't exist anywhere in the list, the generator will be empty, but it won't raise any exception (other than StopIteration ).如果列表中的任何地方都不存在标签,生成器将为空,但不会引发任何异常( StopIteration除外)。

You can get the items between the values by utilizing the index function to search for the index of both objects in the list.您可以通过使用index function 搜索列表中两个对象的索引来获取值之间的项目。 Be sure to add one to the index of the first object so it won't be included in the result.请务必在第一个 object 的索引中添加一个,这样它就不会包含在结果中。 See my code below:请参阅下面的代码:

def get_sublist_between(e1, e2, li): 
    return li[li.index(e1) + 1:li.index(e2)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM