如何在python中删除元组中的引号和括号以格式化数据 [英] How to remove quotes and the brackets within a tuple in python to format the data

查看:125
本文介绍了如何在python中删除元组中的引号和括号以格式化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅打印出现次数最多的字符及其计数.

I am trying to print only the maximum occurring character and its count.

import collections

s = raw_input()
k = (collections.Counter(s).most_common(1)[0])

对于列表,我们有 strip "".join 方法,但是如何以相反的方式处理元组,即删除引号和括号.

for lists, we have strip "".join method but how to deal with tuple the opposite way, i.e., removing the quotes and bracket.

所以,这是我想要的输出没有引号和括号

So, here is what I want the output to be without quotes and brackets

input = "aaabucted"

output = ('a', 3)

我希望输出为 a, 3.

推荐答案

引号不在数据中,它们只是在屏幕上显示内容时添加的.如果您打印值而不是元组的字符串表示形式,您将看到数据中没有引号或括号.所以,问题不在于如何删除引号和括号?"而是如何按照我想要的方式格式化数据?".

The quotes aren't in the data, they are just added when displaying the content on the screen. If you print the value rather than the string representation of the tuple you'll see there are no quotes or brackets in the data. So, the problem isn't "how do I remove the quotes and brackets?" but rather "how do I format the data the way I want?".

例如,使用您的代码,您可以看到没有引号和括号的字符和计数,如下所示:

For example, using your code you can see the character and the count without the quotes and brackets like this:

print k[0], k[1]  # python 2
print(k[0], k[1]) # python 3

当然,您可以使用字符串格式:

And, of course, you can use string formatting:

print "%s, %i" % k   # python 2
print("%s, %i" % k)  # python 3

这篇关于如何在python中删除元组中的引号和括号以格式化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆