find the number of occurrence of letter in a txt file using python

前端 未结 2 1233
醉酒成梦
醉酒成梦 2021-01-29 11:44

I need to read the letter from a .txt file and print the number of occurrence in txt file. so far i have been able to print the content in one line but having issue with the cou

2条回答
  •  暖寄归人
    2021-01-29 11:46

    Here you go. Hopefully your professor will ask you to explain your work.

    grades = open('grades.txt').read().split()
    for grade in sorted(set(grades),
                        key=lambda x: ord(x[0])*3-('+'in x)+('-'in x)):
        print ('{} students got {}'.format(grades.count(grade), grade))
    

提交回复
热议问题