Python程式檢查近似字串
Python中的字串是用於表示文字資料的字元序列,用引號括起來。檢查近似字串涉及比較和衡量它們的相似性或差異性,從而可以使用萊文斯坦距離或模糊匹配演算法等技術執行拼寫檢查和近似字串匹配等任務。
在本文中,我們將學習一個Python程式來檢查近似字串。
演示
假設我們已經獲取了一個**輸入字串**
輸入
Input string 1: aazmdaa Input string 2: aqqaccd k: 2
輸出
Checking whether both strings are similar: True
在這個例子中,'a'在字串1中出現4次,在字串2中出現2次,4 – 2 = 2,在範圍內,類似地,所有字元都在範圍內,因此為真。
使用的方法
以下是完成此任務的各種方法
使用for迴圈、ascii_lowecase、字典推導式和abs()函式
使用Counter()和max()函式
使用for迴圈、ascii_lowecase、字典推導式和abs()函式
在這種方法中,我們將學習如何使用簡單的for迴圈、ascii_lowecase、字典推導式和abs()函式來檢查相似字串。
字典推導式語法
{key_expression: value_expression for item in iterable}
字典推導式是Python中一種緊湊且簡潔的方法,用於透過迭代可迭代物件並根據表示式定義鍵值對來建立字典,從而實現高效且易讀的程式碼。
abs()函式語法
abs(number)
Python中的abs()函式返回數字的絕對值,即不考慮其符號的數值。它對於獲取給定數字的大小或到零的距離很有用。
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟
使用import關鍵字從string模組匯入**ascii_lowercase**。
建立一個函式**findFrequency()**,該函式透過接受輸入字串作為引數來返回字串字元的頻率。
獲取一個字典,並用所有小寫字母作為鍵,值為0填充它。
使用**for迴圈**遍歷輸入字串。
將當前字元的頻率增加1。
返回字元的頻率。
建立一個變數來儲存**輸入字串1**。
建立另一個變數來儲存**輸入字串2**。
列印兩個輸入字串。
建立另一個變數來儲存**輸入k**值
呼叫上述findFrequency()函式,透過傳遞輸入字串作為引數來獲取輸入字串1的字元頻率。
同樣,獲取輸入字串2的字元頻率。
將結果初始化為**True**。
使用**for迴圈**遍歷小寫字母。
使用**if條件**語句檢查兩個字串的當前字元頻率的絕對差是否大於k,並使用**abs()**函式(返回數字的絕對值)。
如果條件為**true**,則將結果更新為**False**。
中斷迴圈。
列印結果。
示例
以下程式使用for迴圈、ascii_lowecase、字典推導式和abs()函式返回給定字串是否近似相同。
# importing ascii_lowercase from the string module from string import ascii_lowercase # creating a function that returns the frequency of characters of # of string by accepting input string as an argument def findFrequency(inputString): # Take a dictionary and filling with all lowercase alphabets as keys # With values as 0 frequency = {c: 0 for c in ascii_lowercase} # Traversing in the given string for c in inputString: # Incrementing the character frequency by 1 frequency[c] += 1 # returning the frequency of characters return frequency # input string 1 inputString_1 = 'aazmdaa' # input string 2 inputString_2 = "aqqaccd" # printing the input strings print("Input string 1: ", inputString_1) print("Input string 2: ", inputString_2) # input K value K = 2 # getting the frequency of characters of input string 1 # by calling the above findFrequency() function stringFrequency1 = findFrequency(inputString_1) # getting the frequency of characters of input string 2 stringFrequency2 = findFrequency(inputString_2) # Initializing the result as True result = True # traversing through all the lowercase characters for c in ascii_lowercase: # checking whether the absolute difference # of frequency of current characters of both strings is greater than k if abs(stringFrequency1[c] - stringFrequency2[c]) > K: # updating False to the result if the condition is true result = False # break the loop break # printing the result print("Checking whether both strings are similar: ", result)
輸出
執行上述程式後,將生成以下輸出
Input string 1: aazmdaa Input string 2: aqqaccd Checking whether both strings are similar: True
使用Counter()和max()函式
在這種方法中,我們將使用Counter和max函式的組合來檢查與給定字串幾乎相似的字串。
**Counter()**函式:一個子類,用於計算可雜湊物件。它在被呼叫/呼叫時隱式地建立可迭代物件的雜湊表。
counter_object = Counter(iterable)
演算法(步驟)
以下是執行所需任務應遵循的演算法/步驟
使用import關鍵字從collections模組匯入**Counter**函式。
建立另一個變數來儲存**輸入k**值
使用**lower()**函式(將字串中的所有大寫字元轉換為小寫字元)將輸入字串1轉換為小寫,然後使用**Counter()**函式獲取輸入字串1的字元頻率。
以同樣的方式,透過首先將其轉換為小寫來獲取輸入字串2的字元頻率。
將結果初始化為**True**。
使用if條件語句檢查字串是否相似。
**max()**方法(返回可迭代物件中值最高的專案/最大數字)
如果條件為**true**,則將結果更新為**False**。
列印結果。
示例
以下程式使用counter()、max()函式返回給定字串是否近似相同。
# importing Counter from the collections module from collections import Counter # input string 1 inputString_1 = 'aazmdaa' # input string 2 inputString_2 = "aqqaccd" # printing the input strings print("Input string 1: ", inputString_1) print("Input string 2: ", inputString_2) # input K value K = 2 # convertig the input string 1 into lowercase and then # getting the frequency of characters of input string 1 strFrequency_1 = Counter(inputString_1.lower()) # convertig the input string 2 into lowercase and then # getting the frequency of characters of input string 2 strFrequency_2 = Counter(inputString_2.lower()) # Initializing the result as True result = True # Checking whether the strings are similar or not if(max((strFrequency_1 - strFrequency_2).values()) > K or max((strFrequency_2 - strFrequency_1).values()) > K): # updating False to the result if the condition is true result = False # printing the result print("Checking whether both strings are similar: ", result)
輸出
執行上述程式後,將生成以下輸出
Input string 1: aazmdaa Input string 2: aqqaccd Checking whether both strings are similar: True
結論
在本文中,我們學習了兩種不同的方法來檢查近似字串。我們學習瞭如何遍歷小寫字母。使用字典(雜湊)和counter()函式,我們學習瞭如何計算給定字串中每個字元的頻率。