2

Below code is supposed to return the most common letter in the TEXT string in the format:

  • always lowercase
  • ignoring punctuation and spaces
  • in the case of words such as "One" - where there is no 2 letters the same - return the first letter in the alphabet

Each time I run the code using the same string, e.g. "One" the result cycles through the letters...weirdly though, only from the third try (in this "One" example).

text=input('Insert String: ')
def mwl(text):
    from string import punctuation
    from collections import Counter
    for l in punctuation:
        if l in text:
            text = text.replace(l,'')
    text = text.lower()
    text=''.join(text.split())
    text= sorted(text)
    collist=Counter(text).most_common(1)
    print(collist[0][0])
mwl(text)   
3
  • Can you show a sample input? along with the expected output and the result that you actually get when you run this code. Commented Dec 31, 2014 at 13:19
  • 2
    I don't think the values in Counter are sorted, even if you give it sorted input. Commented Dec 31, 2014 at 13:19
  • @Pankaj e.g. "One" returns "o", then "o" again, then "e" and then "n". Should always be "e". Everyone is correct on Counter using dictionary, how should I sort then? Commented Dec 31, 2014 at 13:31

3 Answers 3

10

Counter uses a dictionary:

>>> Counter('one')
Counter({'e': 1, 'o': 1, 'n': 1})

Dictionaries are not ordered, hence the behavior.

Sign up to request clarification or add additional context in comments.

Comments

3

You can get the desired output with OrderedDict replacing the below two lines:

text= sorted(text)
collist=Counter(text).most_common(1)

with:

collist = OrderedDict([(i,text.count(i)) for i in text])
collist = sorted(collist.items(), key=lambda x:x[1], reverse=True)

You also need to import OrderedDict for this.

Demo:

>>> from collections import Counter, OrderedDict
>>> text = 'One'
>>> collist = OrderedDict([(i,text.count(i)) for i in text])
>>> print(sorted(collist.items(), key=lambda x:x[1], reverse=True)[0][0])
O  
>>> print(sorted(collist.items(), key=lambda x:x[1], reverse=True)[0][0])
O    # it will always return O
>>> text = 'hello'
>>> collist = OrderedDict([(i,text.count(i)) for i in text])
>>> print(sorted(collist.items(), key=lambda x:x[1], reverse=True)[0][0])
l    # l returned because it is most frequent

1 Comment

Txs used your suggestion, did some research, and modified between key sort and value sort depending on my need - it worked. The best and easiest solution, which worked was from @xnx. txs
1

This can also be done without Counter or OrderedDict:

In [1]: s = 'Find the most common letter in THIS sentence!'
In [2]: letters = [letter.lower() for letter in s if letter.isalpha()]
In [3]: max(set(letters), key=letters.count)
Out[3]: 'e'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.