Letter frequencies · 字母频率
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
This one builds on a lesson you haven't finished yet. Do that lesson first: · 此任务基于您尚未完成的课程。请先完成以下课程:
Python Basics · Python 基础 · Dictionaries (records) · 字典(记录) →
English
Letter frequencies
Return a dictionary that tells you how many times each character appears in a string. For "aab" that is {"a": 2, "b": 1}.
The handy move is freq.get(ch, 0) — it reads the current count for ch, or 0 if you haven't seen it yet. Add 1 and store it back.
中文
字母频率
返回一个字典,告诉你字符串中每个字符出现了多少次。对于 "aab",结果是 {"a": 2, "b": 1}。
一个方便的写法是 freq.get(ch, 0) —— 它读取 ch 当前的计数,如果还没见过就返回 0。加上 1 再存回去。
Write char_frequency(s) that returns a dictionary mapping each character in s to the number of times it appears. · 编写 char_frequency(s),返回一个字典,把 s 中的每个字符映射到它出现的次数。
Click Run to see the output here. · 点击“运行”查看此处输出。