【发布时间】:2021-10-10 16:39:23
【问题描述】:
我想创建一个python函数来检查给定的字符串是否是字谜,如果只有一个单词不匹配,则此代码有效,即如果字符串不好并且爸爸它返回'b'并且'd' 但如果字符串是 'zippo' 和 'hipps' 它只返回 z 和 h,我应该如何修改代码以返回所有不匹配的值。
def anagram(str_1, str_2):
'''
This function check if two string are anagram,
if yes then prints yes otherwise it checks
for the words that need to be deleted to make it an anagram.
'''
if sorted(str_1) == sorted(str_2):
return "The given strings are anagrams"
else:
# words_needed_to_removed = []
zipped_strings = zip(str_1,str_2)
for (i,j) in zipped_strings:
if i!=j:
return i,j
# words_needed_to_removed.append((i,j))
# return f"The words needed to be removed to make the strings an anagram are:{words_needed_to_removed}"
【问题讨论】:
标签: python python-3.x list for-loop anagram