gpt4 book ai didi

python - 如果匹配可以是单个字符串或字符串元组,如何测试字符串中是否存在匹配?

转载 作者:太空宇宙 更新时间:2023-11-03 17:55:44 26 4
gpt4 key购买 nike

我正在尝试从文本文件中进行直接字符串匹配。有时,匹配意味着一个字符串包含多个目标字符串。目前我的代码看起来像

interesting_matches = [
"sys/bootdisk.py",
" engine stalled for ",
" changed to stalled)",
"DSR failure",
"Detected IDI failure",
"idi_shallow_verify_failure",
"Malformed block history",
"Out of order sequence message on",
"Port reset timeout of",
"gmp_info",
"test_thread",
" panic @ time ",
": *** FAILED ASSERTION",
"filesystem full",]

for match in interesting_matches:
# Iterate through simple matches.
if match in line:
processed_line_data = self._process_line(
match,
line,
line_datetime,
line_num,
current_version)

if "kern_sig" in line and "pid" in line:
processed_line_data = self._process_line(
("kern_sig", "pid"),
line,
line_datetime,
line_num,
current_version)

if "vfs_export" in line and "ignoring" in line:
processed_line_data = self._process_line(
("vfs_export", "ignoring"),
line,
line_datetime,
line_num,
current_version)

if "job_d" in line and
"State transition from state " in line and
" took longer than " in line:
processed_line_data = self._process_line(
(
"job_d",
"state transition from state",
" took longer than "),
line,
line_datetime,
line_num,
current_version)

if processed_line_data is not None:
return_list.append(processed_line_data)

我想做的是类似的事情

interesting_matches = [
"sys/bootdisk.py",
" engine stalled for ",
" changed to stalled)",
"DSR failure",
"Detected IDI failure",
"idi_shallow_verify_failure",
"Malformed block history",
"Out of order sequence message on",
"Port reset timeout of",
"gmp_info",
"test_thread",
" panic @ time ",
": *** FAILED ASSERTION",
"filesystem full",
("kern_sig", "pid"),
("vfs_export", "ignoring"),
("job_d", "State transition from state", " took longer than "),]

for matches in interesting_matches
if any(match in line for match in matches):
processed_line_data = self._process_line(
match,
line,
line_datetime,
line_num,
current_version)

但是元组和字符串的混合会导致值错误,指出您无法比较字符串和元组。

如果我想检查单个和多个字符串,如何编写单个比较?

编辑:

这是基于 Sean 回答的工作代码

interesting_matches = [
("sys/bootdisk.py",),
(" engine stalled for ",),
(" changed to stalled)",),
("DSR failure",),
("Detected IDI failure",),
("idi_shallow_verify_failure",),
("Malformed block history",),
("Out of order sequence message on",),
("Port reset timeout of",),
("gmp_info",),
("test_thread",),
(" panic @ time ",),
(": *** FAILED ASSERTION",),
("filesystem full",),
("kern_sig", "pid"),
("vfs_export", "ignoring"),
("job_d", "State transition from state", " took longer than "),]

for matches in interesting_matches:
if all(match in "test_thread" for match in matches):
print(matches)

最佳答案

非常可行。试试这个:对于单子(monad)字符串签名,无论如何将它们包装在一个元组中。现在你的列表是同质的,你的问题变得简单多了。对于每个签名元组,检查签名中的所有子字符串是否也在该行中。

关于python - 如果匹配可以是单个字符串或字符串元组,如何测试字符串中是否存在匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28471637/

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