Python 英文句子字符统计程序 - 小写字母出现次数及总数
def count_characters(sentence):\n count = [0] * 26\n total = 0\n\n for char in sentence:\n if char.islower():\n count[ord(char) - ord('a')] += 1\n total += 1\n\n result = []\n for i in range(26):\n if count[i] > 0:\n result.append(chr(i + ord('a')) + ':' + str(count[i]))\n\n return ','.join(result) + '\nTotal:' + str(total)\n\n\nsentence = input("请输入一句英文句子:")\nprint(count_characters(sentence))
原文地址: https://www.cveoy.top/t/topic/pnuC 著作权归作者所有。请勿转载和采集!