简体   繁体   English

检查python中列表的元素中是否存在列表中的任何元素

[英]Check if any element in a list is present in the elements of a list in python

I'm trying to parse a log file for certain occurrences. 我正在尝试分析某些情况下的日志文件。 As the log file can be quiet big it is necessary to filter out the lines that are of no interest for the application we need it for. 由于日志文件可能很小,因此有必要过滤掉与我们需要它的应用程序无关的行。 the idea was that I make one list with the 4 or 5 strings that I want to look for and then loop over the lines in an other list that contains the lines that I have retained of the log file. 这个想法是,我用要查找的4或5个字符串组成一个列表,然后在另一个包含我已保留的日志文件行的列表中循环遍历。

The log file is the log of a proxy and is used to get a view of where requests come from The first reduction was easy by looking for "GET /" in the line and only store the ones that do have that in it. 日志文件是代理的日志,用于查看请求的来源。第一种减少方法很容易,只需在该行中查找“ GET /”,然后仅将包含请求的内容存储在该行中即可。

with open('logfile', 'r') as f:
    for line in f:
        if "GET /" in line:
           lines.append(line)

The list 'lines' needs then to be reduce to the lines that contain one of a number of strings in the url 然后需要将列表“行”减少为包含url中多个字符串之一的行

l1 = ['/Treintickets/aankopen', '/booking/Tickets', '/Acheter/Billets', ...]

I tried list comprehension but that did not work: 我尝试了列表理解,但是没有用:

result = [l for l in lines if l1 in l]

Is there a way to get this to work without having to loop over the big list lines for each member of 'l1'? 有没有一种方法可以使它工作而不必为“ l1”的每个成员遍历大清单行?

You can use the built-in function any : 您可以使用any内置函数:

result = [line for line in lines if any(substring in line for substring in l1)]

Or, you might consider to use a regex for this. 或者,您可以考虑为此使用正则表达式。

Wim's answer is excellent and does specify the correct way to fix the comprehension. Wim的回答非常棒,并且确实指定了解决问题的正确方法。

Though, I would suggest using a generator expression rather than a comprehension if the input text file is very big! 但是,如果输入文本文件很大,我建议使用生成器表达式而不是理解器! This will prevent Python from loading the entire file into memory. 这样可以防止Python将整个文件加载到内存中。

with open(<file>, "r") as fin:
    generator = (line for line in fin if any(substr in line for substr in l1))
    for res in generator:
         # Handle result found

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

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