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

后端 未结 2 1691
时光取名叫无心
时光取名叫无心 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:08

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

    ",".join([str(s) for s in list(k)])
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题