实现统计字符串中的单词和单词出现的次数: 要求如下: (1)统计的语句为:to be or not to be that is a question (2)要求统计该句中出现的所有单词和其出现次数。 (3)使用字典进行输出,格式如下(注意:排列顺序可以不一致,但是统计的次数要正确): {'to': 2, 'be': 2, 'or': 1, 'not': 1, 'is': 1, 'a': 1, 'question': 1, 'that': 1} 友情提示: 考试平台暂不支持编程题程序在线运行。
正确答案:参考程序(Python):s='to be or not to be that is a question';d={};for w in s.split():d[w]=d.get(w,0)+1;print(d)。核心:用split按空格切词,字典计数。
试卷来源:202203 青少年软件编程等级考试Python二级真题