这批词里哪个出现得最多
查得多
写得多
一批搜索词:苹果、香蕉、苹果、梨、香蕉、苹果。运行下面这段程序:
def word_count(words):
c = {}
for w in words:
c[w] = c.get(w, 0) + 1
return c
def top_word(words):
c = word_count(words)
best = ""
for w in c:
if best == "" or c[w] > c[best]:
best = w
return best
print(top_word(["苹果", "香蕉", "苹果", "梨", "香蕉", "苹果"]))
全部评论