我正在尝试仅打印出现的最大字符及其数量。
import collections
s = raw_input()
k = (collections.Counter(s).most_common(1)[0])
Run Code Online (Sandbox Code Playgroud)
对于列表,我们有strip "".join方法,但是如何以相反的方式处理元组,即删除引号和括号。
所以,这就是我希望输出不带引号和括号的结果
input = "aaabucted"
output = ('a', 3)
Run Code Online (Sandbox Code Playgroud)
我希望输出是a, 3。
引号不在数据中,它们只是在屏幕上显示内容时添加的。如果您打印值而不是元组的字符串表示形式,您将看到数据中没有引号或括号。所以,问题不在于“如何删除引号和括号?” 而是“如何按照我想要的方式格式化数据?”。
例如,使用您的代码,您可以看到没有引号和括号的字符和计数,如下所示:
print k[0], k[1] # python 2
print(k[0], k[1]) # python 3
Run Code Online (Sandbox Code Playgroud)
而且,当然,您可以使用字符串格式:
print "%s, %i" % k # python 2
print("%s, %i" % k) # python 3
Run Code Online (Sandbox Code Playgroud)
您可以列出并加入列表,首先将其全部转换为字符串:
",".join([str(s) for s in list(k)])
Run Code Online (Sandbox Code Playgroud)