Python 检测几乎相似的字符串

Python 检测几乎相似的字符串

在Python中,字符串是由字符序列组成的,用于表示文本数据,用引号括起来。检测几乎相似的字符串涉及比较和测量它们的相似性或不相似性,可以实现拼写检查和近似字符串匹配等任务,使用Levenshtein距离或模糊匹配算法等技术。

在本文中,我们将学习一个Python程序来检测几乎相似的字符串。

示例

假设我们输入了一个字符串。

输入

Input string 1:  aazmdaa
Input string 2:  aqqaccd
k: 2

Output

Checking whether both strings are similar:  True

在这个例子中,在string1中’a’出现了4次,在string2中出现了2次,4-2=2,在范围内,同样的,所有的字符都在范围内,因此为真。

使用的方法

以下是完成此任务的各种方法:

  • 使用for循环,ascii_lowercase,字典推导和abs()函数

  • 使用Counter()和max()函数

使用for循环,ascii_lowercase,字典推导和abs()函数

在这种方法中,我们将学习如何使用简单的for循环,ascii_lowercase,字典推导和abs()函数来检查相似的字符串。

字典推导语法

{key_expression: value_expression for item in iterable}

字典推导是Python中一种紧凑且简洁的方法,通过迭代可迭代对象并根据表达式定义键值对来创建字典,以实现高效且易读的代码。

abs()函数语法

abs(number)

Python中的abs()函数返回一个数的绝对值,即忽略其符号的数值。它可用于获取给定数的大小或与零的距离。

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字从字符串模块中导入ascii_lowercase。

  • 创建一个名为findFrequency()的函数,通过接受输入字符串作为参数返回字符串字符的频率。

  • 创建一个字典,并将所有小写字母作为键,值为0。

  • 使用for循环遍历输入字符串。

  • 将当前字符的频率增加1。

  • 返回字符的频率。

  • 创建一个变量用于存储输入字符串1。

  • 创建另一个变量用于存储输入字符串2。

  • 打印两个输入字符串。

  • 创建另一个变量用于存储输入k的值。

  • 调用上述findFrequency()函数,通过将输入字符串作为参数传递,以获取输入字符串1的字符频率。

  • 类似地,获取输入字符串2的字符频率。

  • 将结果初始化为True。

  • 使用for循环遍历小写字母。

  • 使用if条件语句检查当前字符串的频率之差的绝对值是否大于k,并使用abs()函数(返回一个数的绝对值)。

  • 如果条件为真,则将结果更新为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()方法(返回可迭代对象中最大值的项/最大数)

  • 如果条件为真,则将结果更新为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

结论

在本文中,我们学习了2种不同的方法来检查几乎相似的字符串。我们学会了如何遍历小写字母表。使用字典(哈希)和counter()函数,我们学会了如何计算给定字符串中每个字符的频率。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程