How to remove quotes and the brackets within a tuple in python to format the data

后端 未结 2 1690
时光取名叫无心
时光取名叫无心 2021-01-29 15:23

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


        
2条回答
  •  不知归路
    2021-01-29 16:18

    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
    

提交回复
热议问题