简体   繁体   English

从列表中删除特定关键字之前的项目

[英]Delete items from a list before a specific keyword

We scraped website data into a list with many irrelevant data in the beginning.一开始,我们将网站数据抓取到一个包含许多不相关数据的列表中。 How do we delete rows containing strings and floats until a specific keyword upon which it should keep the data in the list.我们如何删除包含字符串和浮点数的行,直到它应该将数据保留在列表中的特定关键字。 For example:例如:

We call our list ls with values like this:我们用这样的值调用我们的列表ls

None
bla2 
'11111.3434'
bla3
'1.43'
**Keyword** 
'1.43332'
Bla 4

We want to delete everything before the keyword and keep everything after the keyword in the same list structure.我们希望删除关键字之前的所有内容,并将关键字之后的所有内容保留在相同的列表结构中。

It might be a very easy task, but we're just getting started with Python and got stuck with this part and couldn't find a suitable answer yet.这可能是一项非常容易的任务,但我们才刚刚开始使用 Python 并被这部分卡住了,还没有找到合适的答案。

Thank you in advance!先感谢您!

Use list.index() to get the index, and del to delete使用list.index()获取索引,使用del删除

l = [None, 'bla2', '11111.3434', 'bla3', '1.43', 'Keyword', '1.43332','Bla 4']
del l[0: l.index('Keyword')]

print(l)
['Keyword', '1.43332', 'Bla 4']

You can use itertools.dropwhile :您可以使用itertools.dropwhile

import itertools
l = list(itertools.dropwhile(l, lambda i: i!=keyword))

Or you can use any filtering expression you like for the lambda.或者您可以对 lambda 使用您喜欢的任何过滤表达式。

Can try using slicing the list, an example can be this one可以尝试使用切片列表,一个例子可以是这个

filter_list = lambda string_list, target_keyword: string_list[string_list.index(target_keyword)+1:]

After only need to call the method to perform the filter like this:之后只需要调用方法来执行过滤,如下所示:

your_var = filter_list(x, 'Keyword')

Full example code完整的示例代码

x = [None, 'bla2', 'test3', 'qweqw', 'eje', 'bu']

filter_list = lambda string_list, target_keyword: string_list[string_list.index(target_keyword)+1:]

print(filter_list(x, 'test3'))

since you didn't mention that the key word can repeat itself it can be done with a single for loop因为您没有提到关键字可以重复,所以可以使用单个 for 循环来完成

ls = [None, 'bla2', '11111.3434', 'bla3', '1.43', 'imakeyword', '1.43332', 'Bla4']
keyword = 'imakeyword'
for item in ls:
    if item == keyword:
        pos = ls.index(item)
        new_ls = ls[pos:]

print(new_ls)

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

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