簡體   English   中英

檢查列表中是否出現多個 SUBstring,而不明確定義 substring

[英]Checking if there is more than one SUBstring occuring in a list, without explicitly defining the substring

我有一個要分析的日志列表。 我已將這些日志放入 Python 的列表中。 我想檢查以確保以下 substring '模式'不會出現超過兩次:

'Processing id xxxxxx' 

其中 xxxxxx 是一個特定的 ID。 基本上,我不希望日志處理同一個 ID超過兩次......它可以處理幾個不同的 id,但是如果同一個 ID一遍又一遍地處理,我想知道。 不知道 ID 會是什么,我只知道我不想重新處理同一個 ID。

我了解如何檢查 substring 是否多次出現,但我不知道如何檢查我當時不完全知道的 id。

# response is the list of logs that I am analyzing.
# substring is the 'Processing id xxxxxx' string.

process_str = [s for s in response if substring in s]
if len(process_str) > 2:
   ## raise a flag here

遍歷日志以獲取處理 id 並將它們存儲在字典中,值為它發生的次數。

ids = {}
for s in response:
    m = re.search(r'(Processing id )(\d{6})', s)
    id = m.group(2)
    
    if id not in ids:
        ids[id] = 1
    else:
        ids[id] += 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM