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

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

在本教程中,我们将讨论用户如何编写一个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: 如果从string_1提取的字符在string_2中找到。如果该字符在string_1中的第一次出现的索引与当前提取的字符的索引相同,则将计数器的值增加1。

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

示例:

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

示例:方法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: 我们将在这两个字符串上使用set(交集)。
  • 步骤3: 我们将使用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模块。
  • 步骤2: 使用re.search()函数来检查string_1中的任何字符是否在string_2中存在,如果存在,则将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程序来计算给定字符串对中匹配字符的数量。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程