新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python 程序:檢查兩個(gè)字符串是否是異序詞
創(chuàng)新互聯(lián)python教程:

寫(xiě)一個(gè) Python 程序來(lái)檢查兩個(gè)字符串是否是異序詞。例如,如果一個(gè)字符串通過(guò)重新排列其他字符串字符而形成,則它是一個(gè)異序詞字符串。例如,三角形和積分都將通過(guò)重新排列字符而形成。
在本 Python 示例中,sorted 方法按字母順序?qū)蓚€(gè)字符串進(jìn)行排序,if 條件檢查兩個(gè)排序后的字符串是否相等。如果為真,則兩個(gè)字符串是異序詞。
str1 = input("Enter the First String = ")
str2 = input("Enter the Second String = ")
if(sorted(str1) == sorted(str2)):
print("Two Strings are Anagrams.")
else:
print("Two Strings are not Anagrams.")Enter the First String = listen
Enter the Second String = silent
Two Strings are Anagrams.Python 程序檢查兩個(gè)字符串是否是異序詞或者沒(méi)有使用集合庫(kù)中的計(jì)數(shù)器。
from collections import Counter
str1 = input("Enter the First String = ")
str2 = input("Enter the Second String = ")
if(Counter(str1) == Counter(str2)):
print("Two Strings are Anagrams.")
else:
print("Two Strings are not Anagrams.")Enter the First String = race
Enter the Second String = care
Two Strings are Anagrams.
Enter the First String = dare
Enter the Second String = care
Two Strings are not Anagrams. 網(wǎng)站題目:Python 程序:檢查兩個(gè)字符串是否是異序詞
分享路徑:http://m.5511xx.com/article/dhspdhd.html


咨詢
建站咨詢
