-4

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])

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)

I want the output to be a, 3.

3
  • You can pass a list or tuple (or some other things) to join(). Commented Oct 23, 2015 at 13:57
  • 2
    It is unclear what you are asking here. Please use the edit link to your question to show the output of your actual code and the expected result.
    – Sede
    Commented Oct 23, 2015 at 13:58
  • So you want to print 'a', 3?
    – Noxeus
    Commented Oct 23, 2015 at 14:00

2 Answers 2

7

You can make a list and join it, first converting all to strings:

",".join([str(s) for s in list(k)])
0
5

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
2
  • I like print "%s, %i" % k!
    – Noxeus
    Commented Oct 23, 2015 at 14:02
  • e.g. print(' '.join("%s, %i" % v + '\n' for v in k.most_common(10)))
    – Alex
    Commented Aug 25, 2017 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.