统计字符串中数字、字母、其他字符的出现次数 |
您所在的位置:网站首页 › python输入一个字符 › 统计字符串中数字、字母、其他字符的出现次数 |
【问题描述】从键盘输入一个字符串,分别统计数字,字母(包括大小写)和其他字符的个数,输出每个字符及其个数。要求:用字典进行统计。 【输入形式】输入一个随机字符串 【输出形式】输出为记录统计的结果,保持数字、字母、其他的顺序 【样例输入】afahiubuio.,.,1.,4.1 【样例输出】数字3,字母8,其他9 【样例说明】 【评分标准】 a = input() b = len(a) n = {"数字": 0, "字母": 0, "其他": 0} word = [] math = [] other = [] for i in range(0, b): l = a[i].isalpha() # 字母 m = a[i].isdigit() # 数字 if l == True: word.append(a[i]) else: if m == True: math.append(a[i]) else: other.append(a[i]) d = len(word) f = len(math) g = len(other) n["数字"] = f n["字母"] = d n["其他"] = g print("数字{},字母{},其他{}".format(n["数字"], n["字母"], n["其他"]))(根据系统的正误指示,对此进行如下修改) a = input() b = len(a) n = {"数字": 0, "字母": 0, "其他": 0} word = [] math = [] other = [] for i in range(0, b): l = a[i].isalpha() # 字母 m = a[i].isdigit() # 数字 if l == True: word.append(a[i]) else: if m == True: math.append(a[i]) else: other.append(a[i]) d = len(word) f = len(math) g = len(other) if f!=0: n["数字"] = f print("数字{},".format(n["数字"]),end="") if d!=0: n["字母"] = d print("字母{},".format(n["字母"]),end="") if g!=0: n["其他"] = g print("其他{}".format(n["其他"]),end="") |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |