简体   繁体   English

Python - 如何比较两个列表的内容在一个字符串中

[英]Python - How to compare the content of two list are in a string

I need to compare that all the items of list "present" are in the string "line" and all the elements of the list "absent" are NOT in the string "line"我需要比较列表“present”的所有项目都在字符串“line”中,并且列表“absent”的所有元素都不在字符串“line”中

So, having the 2 lists因此,拥有 2 个列表

present = ['SYN', 'ACK']
absent = ['RST', 'FIN']

And a file with all the TCP flags from here: https://github.com/robcowart/elastiflow/blob/master/logstash/elastiflow/dictionaries/tcp_flags.yml还有一个包含所有 TCP 标志的文件: https://github.com/robcowart/elastiflow/blob/master/logstash/elastiflow/dictionaries/tcp_flags.yml

"...
"12": RST-PSH
"13": FIN-RST-PSH
"14": SYN-RST-PSH
"15": FIN-SYN-RST-PSH
"16": ACK
"17": FIN-ACK
"18": SYN-ACK
"19": FIN-SYN-ACK
"20": RST-ACK
"21": FIN-RST-ACK
"22": SYN-RST-ACK
"23": FIN-SYN-RST-ACK
..."

I will read the file line by line if all the elements of "present" exist in the line and all the elements of "absent" do NOT exist in the line, then print the line如果“present”的所有元素都存在于该行中并且“absent”的所有元素都不存在于该行中,我将逐行读取文件,然后打印该行

How should I do it?我该怎么做? I imagine recursion or comprehension, but I can not find the way.我想象递归或理解,但我找不到方法。 thanks谢谢

for line in csv_reader:
    # parse the line and store the flags into a list
    # flags = line.split...

    # the logic to check for present and absent
    is_present = all(elem in flags for elem in present)
    is_absent = not any(elem in flags for elem in absent)
    if is_present and is_absent:
        print(line)
line="abcee"
present=['abc','cde','fgh']
absent=['bla','ghj']
def AllInLine():
    for i in present:
        if i not in line:
            return False;
    return True;
def NoneInLine():
    for i in absent:
        if i in line:
            return False;
    return True;

then if both functions return true you can print the line然后如果两个函数都返回 true 你可以打印该行

Here is something you can try.这是您可以尝试的方法。 It downloads raw YAML file from GitHub using requests , parses the YAML using PyYAML , then checks the existence of each item from absent and present using all() and prints the lines that have all present items in the line and all absent items not in the line.它使用requests从 GitHub 下载原始 YAML 文件,使用PyYAML解析 YAML ,然后使用all()检查每个项目是否absentpresent线。

The YAML file is also downloaded in chunks, just in case it gets big. YAML 文件也以块的形式下载,以防它变大。 This is a good practice when downloading files over HTTP anyways.无论如何,当通过 HTTP 下载文件时,这是一个很好的做法。

Demo:演示:

from pathlib import Path
from requests import get
from yaml import safe_load, YAMLError

def download_file(url, chunk_size=1024):
    with get(url, stream=True) as req:
        filename = Path(url).name
        with open(filename, mode="wb") as f:
            for chunk in req.iter_content(chunk_size=chunk_size):
                if chunk:
                    f.write(chunk)

def parse_yaml_file(path):
    with open(path) as f:
        try:
            yaml_file = safe_load(f)
            return yaml_file
        except YAMLError as ex:
            print(ex)

if __name__ == "__main__":
    present = ['SYN', 'ACK']
    absent = ['RST', 'FIN']

    yaml_file = download_file("https://raw.githubusercontent.com/robcowart/elastiflow/master/logstash/elastiflow/dictionaries/tcp_flags.yml")

    data = parse_yaml_file(yaml_file)

    for number, line in data.items():
        if all(p in line for p in present) and all(a not in line for a in absent):
            print(f"{number}: {line}")

Output: Output:

18: SYN-ACK
26: SYN-PSH-ACK
50: SYN-ACK-URG
58: SYN-PSH-ACK-URG
82: SYN-ACK-ECE
90: SYN-PSH-ACK-ECE
114: SYN-ACK-URG-ECE
122: SYN-PSH-ACK-URG-ECE
146: SYN-ACK-CWR
154: SYN-PSH-ACK-CWR
178: SYN-ACK-URG-CWR
186: SYN-PSH-ACK-URG-CWR
210: SYN-ACK-ECE-CWR
218: SYN-PSH-ACK-ECE-CWR
242: SYN-ACK-URG-ECE-CWR
250: SYN-PSH-ACK-URG-ECE-CWR

Note: The above will probably need more error checking for your scenario, but it shows the general idea.注意:上面可能需要对您的场景进行更多的错误检查,但它显示了总体思路。

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

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