Finding the most frequent character in a string

前端 未结 10 1918
执笔经年
执笔经年 2020-12-03 21:54

I found this programming problem while looking at a job posting on SO. I thought it was pretty interesting and as a beginner Python programmer I attempted to tackle it. Howe

10条回答
  •  长情又很酷
    2020-12-03 22:30

    Here is way to find the most common character using a dictionary

    message = "hello world"
    d = {}
    letters = set(message)
    for l in letters:
        d[message.count(l)] = l
    
    print d[d.keys()[-1]], d.keys()[-1]
    

提交回复
热议问题