简体   繁体   English

检查以短语 = A,B 给出的输入对中的字谜,其中 A 和 B 是 2 个单词

[英]check for anagram in pair of input given as phrase = A, B. where A and B are 2 words

this is the code I wrote.这是我写的代码。 but it gives True/False for each alphabet.但它为每个字母表给出真/假。 I want the output to give me a single true or false.我希望输出给我一个真或假。 what changes should I make?我应该做哪些改变?

Z = input()
Y = Z.split()
M = list(Y[0])
N = list(Y[1])

M.pop(-1)

for i in range(len(N)):
    print(M.count(M[i]) == N.count(N[i]))  

I would take another approach.我会采取另一种方法。 If you sort the letters of both words and compare them against each other, then you will get a single True if the words are anagrams of each other:如果您对两个单词的字母进行排序并将它们相互比较,那么如果单词是彼此的字谜,您将得到一个True

>>> def is_anagram(word1, word2):
...   return sorted(word1) == sorted(word2)
>>> is_anagram('elbow', 'below')
>>> True
>>> is_anagram('elbow', 'lower')
>>> False

Check my code:检查我的代码:

A = "a decimal point".replace(" ","")
B = "i m a dot in place".replace(" ","")


def checkAnagram(A,B):
    for i in range(len(A)):
        
        if(A.count(A[i])!=B.count(A[i])):
            return False
    return True

         
if(len(A)!=len(B)):
        print("Pair is not anagram.")
else:
    if(checkAnagram(A,B)):
        print("pair is anagram")
    else:
        print("Pair is not Anagram")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该函数接受两个矩阵作为输入,并返回一个A * B的矩阵。在Python中 - A function that takes two matrices as input, and returns a matrix with A * B. In Python Python “如果 a 和 b 或 a 和 c”的语法,其中 a、b 和 c 是句子中的单词 - Python syntax for “if a and b or a and c” where a, b and c are words in a sentence 方法 b. is_palindrom() 不起作用 - the method b. is_palindrom() does not work Ptyhon,如何在给定的字符串中添加一个连字符 b/w 两个单词 - Ptyhon, how to add a hyphen b/w two words in a given string celeryd -B。 如何在后台运行它? - manage.py celeryd -B. How to run it in background? 检索 a 列值,其中主键是元组 (a,b) 并且对于 b' 值的给定列表必须存在所有行 (a,b') - Retrieving the a column value where the primary key is a tuple (a,b) and where all rows (a,b') must exist for a given list for b' values 用户输入中使用了数组 A 和数组 B 中的多少个单词? - How many words from the array A and array B are used in the user input? 检查表 B 中的 X、Y 列对是否在表 A 中任何 X、Y 列对的增量距离内 - Check if X,Y column pair in table B is within delta distance of any X, Y column pair in table A 正则表达式在短语之前检查单词 - Regex to check for words before phrase 给定两个大型 numpy arrays,如何有效地计算两个 arrays 的任何所有(a,b)对的 ab 矩阵? - Given two large numpy arrays, how to efficiently compute a matrix of a-b for any all (a,b) pair of the two arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM