首页 > 编程知识 正文

Python 程序:计算一对字符串中匹配字符数

时间:2023-11-26 13:13:30 阅读:309449 作者:HDER

在本教程中,我们将讨论用户如何编写一个 Python 程序来计算给定字符串对中匹配字符的数量。

我们将传递这对非空字符串。程序将计算该对字符串中匹配字符的数量。这里,我们将考虑我们传递的字符串中有重复的字符。

示例:


Input: string_1 = 'Javtpoint'
strint_2 = 'Juvpionk'
Output: no. matching characters in the pairs of strings: 6
(That is, the matching characters are: - J, v, p, i, o, n)
Input: string_1: 'zyxw531@7#'
string_2: 'xwuv234#'
Output: no. matching characters in the pairs of strings: 4
(That is, the matching characters are: - x, w, 3, #)

方法 1:

  • 步骤 1: 我们将计数器变量初始化为 0。
  • 步骤 2: 我们将从第一个字符到最后一个字符迭代第一个字符串。
  • 步骤 3: 如果从字符串 _1 中提取的字符在字符串 _2 中找到。如果 string_1 中该字符的第一个出现索引与当前提取字符的索引相同,那么它会将计数器的值增加 1。

然后我们将在 Python 中使用 string.find('character ')来查找相同的字符。如果找到,这将返回字符串中字符的第一个出现索引;否则,它将返回“-1”。

示例:


string_1 = 'zyxwvwwv'
string_1find('w') ? 3
string_1find('v') ? 4
string_1find('t') ? -1
  • 第四步:打印计数器的输出值。

例:方法 1


# First, we will define the count function
def count_1(string_1, string_2): 
    count, find_1 = 0, 0

# The loop will execute till the length of string_1 and it will 
# Stores the value of string_1 character by character and stores in "store_1" at every iteration.
    for store_1 in string_1:    

        # This will check if the extracted from of the characters of string_1 
        # is present in string_2 or not.
        if string_2.find(store_1) >= 0 and find_1 == string_1.find(store_1): 
            count += 1
        find_1 += 1
    print ('The no. matching characters in the pairs of strings: ', count)

# Main function
def main(): 
    string_1 = str(input ("Please enter the characters for String 1: "))
    string_2 = str(input ("Please enter the characters for String 2: "))
    count_1(string_1, string_2) # At last, calling the count function

# Driver Code
if __name__ == "__main__":
    main()

输出:

Please enter the characters for String 1:  ajg 78y
Please enter the characters for String 2:  gjy 23r
The no. matching characters in the pairs of strings:  2

方法 2:

  • 步骤 1: 在这个方法中,我们将使用 set()函数来移除给定字符串上的重复。
  • 步骤 2: 我们将在两个字符串上使用集合(交集)。
  • 第三步:我们将使用 len()函数计算“matched_characters_1”字符串的长度。

例 2:方法 2


# First, we will define the count function
def count_1(string_1, string_2): 
    # The set of characters of string_1
    set_string_1 = set(string_1)

    # The set of characters of string2
    set_string_2 = set(string_2)

    # We will use "&" intersection mathematical operation on the sets
    # the unique characters present in both the strings
    # will be stored in matched_characters_1 set variable
    matched_characters_1 = set_string_1 & set_string_2

    #Then, we will print the length of matched_characters_1 set
    # which will give the number of matched characters in the pair of strings.
    Print ('The number matching characters in the pairs of strings: ' + str (len (matched_characters_1)))

# Driver code
if __name__ == "__main__" :

    string_1 = str(input ("Please enter the characters for String 1: "))
    string_2 = str(input ("Please enter the characters for String 2: "))

    count_1(string_1, string_2) # At last, calling the count function

输出:

Please enter the characters for String 1:  awe ret #$65
Please enter the characters for String 2:  rty urw @!34 
The number matching characters in the pairs of strings: 4

方法 3:

  • 步骤 1: 我们将导入 Re 模块。
  • 第二步:我们将使用 re.search()函数检查 string_2 中是否存在 string_1 的任何字符,如果存在,则将 1 添加到计数器变量中。

例 3:方法 3


import re
string_1 = str(input ("Please enter the characters for String 1: "))
string_2 = str(input ("Please enter the characters for String 2: "))

count = 0
for store_1 in string_1:
    if re.search(store_1, string_2):
        count = count + 1
print ('The number matching characters in the pairs of strings: ', count)

输出:

Please enter the characters for String 1: learning
Please enter the characters for String 2: working
The number matching characters in the pairs of strings: 5

结论

在本教程中,我们讨论了编写 Python 程序来计算给定字符串对中匹配字符数量的不同方法。


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。