简体   繁体   中英

Return the most common element as a string not a list python

I know I can use the counter from collections to return the most common elements in an array or a string and so on. However this counter returns a list of the n most common elements and their counts from the most common to the least. Lets say I want to find most common characters in a string using counter:

Counter('abracadabra').most_common(1)

This will however return an answer of type list like this:

[('a', 5)]

Is there a way to return only the character "a" as a type of string without the times it is repeated?

Thanks for the help!

How about just grabbing the string from the output of Counter.most_common()?

something like:

Counter('abracadabra').most_common(1)[0][0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM