在本教程中,我們將討論用戶如何編寫一個 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