class MyString:
def __init__(self, myString):
self.__myString = myString
def countWord(self):
count = len(self.__myString.split())
return count
def findMostFrequentChar(self):
# ?
我需要实现findMostFrequenctChar
函数。她给我们唯一的提示是需要创建两个列表,这就让我感到迷惑了。
以下是调用该函数的代码:
def main():
aString = MyString("This is a super long long long string. Please help count me")
print("There are", aString.countWord(), "words in the string.")
count, letter = aString.findMostFrequentChar()
print("The most frequent character is", letter, "which appeared", count, "times")
main()
collections.Counter
,可以使用字典来计算每个字母的数量。放弃那个使用两个列表的想法。 - timgeb