Python 程序:检查两个字符串是否是异序词

创新互联python教程:

写一个 Python 程序来检查两个字符串是否是异序词。例如,如果一个字符串通过重新排列其他字符串字符而形成,则它是一个异序词字符串。例如,三角形和积分都将通过重新排列字符而形成。

在本 Python 示例中,sorted 方法按字母顺序对两个字符串进行排序,if 条件检查两个排序后的字符串是否相等。如果为真,则两个字符串是异序词。

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 程序检查两个字符串是否是异序词或者没有使用集合库中的计数器。

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.

网站栏目:Python 程序:检查两个字符串是否是异序词
URL分享:http://bzwzjz.com/article/dhspdhd.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 高端网站设计 重庆网站建设 成都网站建设公司 网站设计 成都定制网站建设 成都网站制作公司 成都网站制作 高端网站设计推广 成都网站建设 网站建设方案 成都网站制作 成都网站制作 网站设计 企业手机网站建设 网站建设方案 成都网站设计 成都企业网站建设公司 成都品牌网站建设 上市集团网站建设 重庆网站制作 高端品牌网站建设 盐亭网站设计