gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:55:46 24 4
gpt4 key购买 nike

我需要比较列表“present”的所有项目都在字符串“line”中,而列表“absent”的所有元素都不在字符串“line”中

所以,有 2 个列表

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

还有一个包含此处所有 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
..."

如果该行中存在所有“present”元素并且该行中不存在所有“absent”元素,我将逐行读取该文件,然后打印该行

我该怎么做?我想象递归或理解,但我找不到方法。谢谢

最佳答案

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)

关于Python-如何比较字符串中两个列表的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61176309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com