【发布时间】:2020-11-27 21:37:44
【问题描述】:
此代码按字母顺序打印字符串中出现的字母表以及每个字母出现的次数。此代码中有两个不需要的数字,即 8 和 .1,但我不知道如何从输出中删除它们。
s = 'ThiS is String with Upper and lower case Letters.'
# Or see Python's Counter class: counts = Counter(s.lower())
counts = {}
for c in s.lower():
counts[c] = counts.get(c, 0) + 1
for c, n in sorted(counts.items()):
print(c, n)
【问题讨论】:
-
8 是字符串中“”的空格数。
-
那我猜“1”代表点(
.)。 -
我不希望在输出中出现这种情况,哈哈。我怎样才能改变它,使这两个数字不显示?
标签: python python-3.x