最常见的字母查找器中的逻辑错误

时间:2014-06-21 03:13:48

标签: python logic

所以,这个程序只是让用户输入一个字符串,并计算每个字符的出现次数,然后显示最频繁。

我输入" AABBCCC",它告诉我最大值为7,最常见的是" Q"。

countList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 \
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
index = 0
for character in userInput:
    if character == "Q" or "q":
        countList[0] += 1
    elif character == "W" or "w":
        countList[1] += 1
    elif character == "E" or "e":
        countList[2] += 1
    elif character == "R" or "r":
        countList[3] += 1
    elif character == "T" or "t":
        countList[4] += 1
    elif character == "Y" or "y":
        countList[5] += 1
    elif character == "U" or "u":
        countList[6] += 1
    elif character == "I" or "i":
        countList[7] += 1
    elif character == "O" or "o":
        countList[8] += 1
    elif character == "P" or "p":
        countList[9] += 1
    elif character == "A" or "a":
        countList[10] += 1
    elif character == "S" or "s":
        countList[11] += 1
    elif character == "D" or "d":
        countList[12] += 1
    elif character == "F" or "f":
        countList[13] += 1
    elif character == "G" or "g":
        countList[14] += 1
    elif character == "H" or "h":
        countList[15] += 1
    elif character == "J" or "j":
        countList[16] += 1
    elif character == "K" or "k":
        countList[17] += 1
    elif character == "L" or "l":
        countList[18] += 1
    elif character == "Z" or "z":
        countList[19] += 1
    elif character == "X" or "x":
        countList[20] += 1
    elif character == "C" or "c":
        countList[21] += 1
    elif character == "V" or "v":
        countList[22] += 1
    elif character == "B" or "b":
        countList[23] += 1
    elif character == "N" or "n":
        countList[24] += 1
    elif character == "M" or "m":
        countList[25] += 1
    elif character == "`":
        countList[26] += 1
    elif character == "~":
        countList[27] += 1
    elif character == "1":
        countList[28] += 1
    elif character == "!":
        countList[29] += 1
    elif character == "2":
        countList[30] += 1
    elif character == "@":
        countList[31] += 1
    elif character == "3":
        countList[32] += 1
    elif character == "#":
        countList[33] += 1
    elif character == "4":
        countList[34] += 1
    elif character == "$":
        countList[35] += 1
    elif character == "5":
        countList[36] += 1
    elif character == "%":
        countList[37] += 1
    elif character == "6":
        countList[38] += 1
    elif character == "^":
        countList[39] += 1
    elif character == "7":
        countList[40] += 1
    elif character == "&":
        countList[41] += 1
    elif character == "8":
        countList[42] += 1
    elif character == "*":
        countList[43] += 1
    elif character == "9":
        countList[44] += 1
    elif character == "(":
        countList[45] += 1
    elif character == "0":
        countList[46] += 1
    elif character == ")":
        countList[47] += 1
    elif character == "-":
        countList[48] += 1
    elif character == "_":
        countList[49] += 1
    elif character == "=":
        countList[50] += 1
    elif character == "+":
        countList[51] += 1
    elif character == "[":
        countList[52] += 1
    elif character == "{":
        countList[53] += 1
    elif character == "]":
        countList[54] += 1
    elif character == "}":
        countList[55] += 1
    elif character == "\\":
        countList[56] += 1
    elif character == "|":
        countList[57] += 1
    elif character == ";":
        countList[58] += 1
    elif character == ":":
        countList[59] += 1
    elif character == "'":
        countList[60] += 1
    elif character == "\"":
        countList[61] += 1
    elif character == ",":
        countList[62] += 1
    elif character == "<":
        countList[63] += 1
    elif character == ".":
        countList[64] += 1
    elif character == ">":
        countList[65] += 1
    elif character == "/":
        countList[66] += 1
    elif character == "?":
        countList[67] += 1
mostFrequent = max(countList)
print(mostFrequent)
characterKey = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A" \
, "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M" \
, "`", "~", "1", "!", "2", "@", "3", "#", "4", "$", "5", "%", "6", "^", "7" \
, "&", "8", "*", "9", "(", "0", ")", "-", "_", "=", "+", "[", "{", "]", "}" \
, "\\", "|", ";", ":", "'", "\"", ",", "<", ".", ">", "/", "?"]

index = 0
for character in countList:
    if character == mostFrequent:
        print(characterKey[index], "is most frequent.")
    index += 1

3 个答案:

答案 0 :(得分:2)

您正在处理的问题之一是

elif character == "T" or "t"

不像你想象的那样工作。你必须重新重写表达式的左手边,如下所示:

elif character == "T" or character == "t"

或者您可以使用in运算符:

elif character in ("T,"t")

但是,更改此功能并不能完全解决您的问题。

有助于使代码更清晰,更快的一件事是将所收到的字符值转换为整数值,然后使用整数值作为将字符放入右侧索引的方法。阵列。我会把这个练习留给你:)

答案 1 :(得分:1)

我认为使用dict可以节省一些代码。 .lower()可以防止你必须检查每个,因为我假设你的目的与'T'等效。请注意,辅助函数需要一个非空的字符串。希望这可以帮助

def get_character_frequency(user_input):
    '''return dictionary of frequencies to characters'''  

    char_freq_dict = {}
    freq_char_dict = {}
    #create dict with characters as keys and freqs as values
    for character in user_input:
        char_freq_dict[character] = char_freq_dict.get(character, 0) + 1
    #invert dict
    for character, freq in char_freq_dict.items():
        if freq in freq_char_dict.keys():
            freq_char_dict[freq].append(character)
        else:
            freq_char_dict[freq] = [character]
    return freq_char_dict

def main():
    #you could add some code to ensure some text is entered, or 
    #modify to error check for this

    userInput = input("Enter String: ").lower()
    freq_char_dict = get_character_frequency(userInput)         
    max_freq, max_char = sorted(freq_char_dict.items(), reverse=1)[0]
    print('\n{} characters were entered'.format(len(userInput)))    
    print('Most frequent character(s) {}'.format(max_char), end="  ")
    print('Ocurring {} time(s)'.format(max_freq))

main()

答案 2 :(得分:0)

这会对你有帮助。

import re
import itertools
import operator

def most_common(L):
  # get an iterable of (item, iterable) pairs
  SL = sorted((x, i) for i, x in enumerate(L))
  # print 'SL:', SL
  groups = itertools.groupby(SL, key=operator.itemgetter(0))
  # auxiliary function to get "quality" for an item
  def _auxfun(g):
    item, iterable = g
    count = 0
    min_index = len(L)
    for _, where in iterable:
      count += 1
      min_index = min(min_index, where)
    # print 'item %r, count %r, minind %r' % (item, count, min_index)
    return count, -min_index
  # pick the highest-count/earliest item
  return max(groups, key=_auxfun)[0]

user_input = [_.lower() for _ in raw_input("Enter String: ")]
frequent = most_common(list(user_input))
print "Most frequent entry:",frequent
print "Number of Occerences:",user_input.count(frequent)

输出:

Enter String: aAbBBcccC12309u/.,;'/*
Most frequent entry: c
Number of Occerences: 4