【发布时间】:2013-11-19 21:47:08
【问题描述】:
我对 Python 还很陌生,但仍然无法以我想要的方式显示我拥有的数据。我有这段代码可以确定字符串中最常见的字符。但是,我如何打印它:('A', 3)。
stringToData = raw_input("Please enter your string: ")
import collections
print (collections.Counter(stringToData).most_common(1)[0])
我只是想深入了解如何将这段代码操作成类似这样的东西:
print "In your string, there are: %s vowels and %s consonants." % (vowels, cons)
显然它会说,“在你的字符串中,最常见的字符是 (character),它出现了 (number) 次。”
我使用的是 Python 2.7,并尝试使用 pprint,但我并不真正了解如何将其合并到我现有的代码中。
编辑:基本上,我要问的是如何编码查找字符串中最常见的字符并以诸如“在您的字符串中,最常见的字符是(字符)之类的方式打印它,发生(次数)次。”
【问题讨论】:
-
您希望
pprint在这里为您做什么?它所做的只是调整大型收藏品的打印方式;您根本没有尝试显示集合。
标签: python collections counter pprint