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/zh-tw/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

發表回復

登錄後才能評論