Python 3.x 回文和语法错误?

Python 3.x 回文和语法错误?,python-3.x,Python 3.x,我们如何创建一个程序来检查我们给出的输入(例如:example)是否具有相同的长度和相同的字母(例如)?这是一个高效的python代码 NO_OF_CHARS = 256 def areAnagram(str1, str2): # Create two count arrays and initialize all values as 0 count1 = [0] * NO_OF_CHARS count2 = [0] * NO_OF_CHARS # For e

我们如何创建一个程序来检查我们给出的输入(例如:example)是否具有相同的长度和相同的字母(例如)?

这是一个高效的python代码

NO_OF_CHARS = 256
def areAnagram(str1, str2):

    # Create two count arrays and initialize all values as 0
    count1 = [0] * NO_OF_CHARS
    count2 = [0] * NO_OF_CHARS

    # For each character in input strings, increment count
    # in the corresponding count array
    for i in str1:
        count1[ord(i)]+=1

    for i in str2:
        count2[ord(i)]+=1

    # If both strings are of different length. Removing this
    # condition will make the program fail for strings like
    # "aaca" and "aca"
    if len(str1) != len(str2):
        return 0

    # Compare count arrays
    for i in xrange(NO_OF_CHARS):
        if count1[i] != count2[i]:
            return 0

    return 1
str1 = "EXAMPLE"
str2 = "XAMPLEE"
if areAnagram(str1, str2):
    print "The two strings are anagram of each other"
else:
    print "The two strings are not anagram of each other"
导入数组
word1=“示例”
word2=“示例”
字母=数组。数组('i',(0,)*26)
#数一数单词1的字母
对于word1中的c:
charIndex=ord(c)-ord('A')
字母[charIndex]=字母[charIndex]+1
#计算单词2的字母数并从单词1中减去它们
对于word2中的c:
charIndex=ord(c)-ord('A')
字母[charIndex]=字母[charIndex]-1
#如果单词相同,则字母仅包含0
如果字母数(0)<26:
打印(“文字不同”)
其他:
打印(“单词是字谜”)

请向我们提供您编写和尝试的代码,并更具体地说明您在使用此代码时遇到的问题。
import array

word1 = "EXAMPLE"
word2 = "XAMPLEE"
letters = array.array('i',(0,)*26)

# Count letters of word1
for c in word1:
  charIndex = ord(c) - ord('A')
  letters[charIndex] = letters[charIndex]+1

# Count letters of word2 and substract them from word1 count
for c in word2:
  charIndex = ord(c) - ord('A')
  letters[charIndex] = letters[charIndex]-1

# letters contains only 0 if words are the same
if letters.count(0) < 26:
  print("Words are different")
else:
  print("Words are anagrams")