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: 如果从字符串 _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 程序来计算给定字符串对中匹配字符数量的不同方法。


原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/257056.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-15 12:43
下一篇 2024-12-15 12:43

相关推荐

  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • 英语年龄用连字符号(Hyphenation for English Age)

    英语年龄通常使用连字符号表示,比如 “five-year-old boy”。本文将从多个方面探讨英语年龄的连字符使用问题。 一、英语年龄的表达方式 英语中表…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29

发表回复

登录后才能评论