python问题求答案啊!急!

python问题求答案啊!急!
PROBLEM: Given a sentence (up to 1024 characters long), output the following:
#1) The number of different letters. This will be a number from 1 to 26, inclusive.
2) The number of vowels. Vowels are the letters a, e, i, o, and u.
3) The number of uppercase letters.
4) The number of times that the most frequent letter appears. There is no distinction between
lowercase and uppercase letters.
5) The longest word in the sentence. If there is a tie, print the one that appears first when sorting
these words alphabetically without regard to lowercase and uppercase.
INPUT: One line of data, containing a sentence, up to 1024 characters long.
OUTPUT: Print the five statistics specified above in that order.
SAMPLE INPUT
The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog.
SAMPLE OUTPUT
#1. 25
2. 19
3. 3
4. 6
5. Roxanne

# 获取全部不同的字母个数
def get_diff_letter(input_str):
    return len(set(list(filter(str.isalpha, input_str))))


# 获取元音字母次数
def get_num_of_vowel(input_str):
    print(sum([1 for letter in input_str if letter.lower() in ['a', 'e', 'i', 'o', 'u']]))


# 大写字母开头的单词个数
def num_of_upper(input_str):
    print(sum([1 for letter in input_str if letter.isupper()]))


# 出现次数最多的字母
def word_of_freq(input_str):
    most_freq = 0
    for letter in input_str:
        num_letter = input_str.count(letter)
        most_freq = max(most_freq, num_letter)
    print(most_freq)


# 最长的单词
def longest(input_str):
    input_str = input_str.replace(',', '').replace('.', '') # 过滤掉英文逗号和句号
    words = input_str.split(' ')
    print(max(words, key=len))


if __name__ == '__main__':
    input_str = 'The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog.'
    get_diff_letter(input_str)
    get_num_of_vowel(input_str)
    num_of_upper(input_str)
    word_of_freq(input_str)
    longest(input_str)

SOLUTION: #1. Count the different letters in the sentence. letters = set(list(filter(str.isalpha, sentence))) num_letters = len(letters) #2. Count the vowels. vowels = sum([1 for letter in letters if letter.lower() in ['a','e','i','o','u']]) #3. Count the uppercase letters. upper_case = sum([1 for letter in letters if letter.isupper()]) #4. Count the most frequent letter. most_freq = 0 for letter in letters: num_letter = sentence.count(letter) most_freq = max(most_freq, num_letter) #5. Find the longest word. words = sentence.split(' ') longest_word = max(words, key=len)

print(num_letters, vowels, upper_case, most_freq, longest_word)


from collections import Counter
input_str="The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog."
vowels = ['a','e','i','o','u']
result = dict(Counter(input_str))
del result[',']
del result['.']
del result[' ']


#1) The number of different letters. This will be a number from 1 to 26, inclusive.
sum_letter = len( set( [ i.lower() for i in list(result.keys()) ] ) )
print(sum_letter)

# 2) The number of vowels. Vowels are the letters a, e, i, o, and u.
sum_vowel = 0
for i in vowels:
    sum_vowel += result.get(i,0)
print(sum_vowel)

# 3) The number of uppercase letters.
sum_upper = 0
for i in result.keys():
    if i.isupper():
        sum_upper += 1
print(sum_upper)

# 4) The number of times that the most frequent letter appears. There is no distinction between lowercase and uppercase letters.
sum_freq = max( result.values() )
print(sum_freq)

# 5) The longest word in the sentence
words = input_str.replace(',',' ').replace('.',' ').split()
longest_word = max( words,key=len )
print(longest_word)






@724 工作者, @johnny233 您们的答案都很好,已关注,。采纳johnny233的答案,724工作者对不起,但已关注