Anfieldisonfire
Anfieldisonfire

Reputation: 11

Simple Dictionary

The question asks to write a function creating a dictionary with the count of each word in the string and only remove the punctuation if it is the last character in the word. I've been trying to solve the punctuation part of the problem. For the assignment I need to identify the punctuation using isalpha, but

  1. I'm not sure if using the word[-1] is helping to identify if the last character is punctuation and
  2. I don't know how to write the else statement to get the full dictionary to produce.
def word_distribution(s):

    s = s.lower()
    s = s.split()
    wordfreq = {}
    for word in s:
        if word[-1].isalpha():
            if word in wordfreq:
                wordfreq[word] += 1 
            else:
                wordfreq[word] = 1

    return wordfreq

Example of what my code is producing...

word_distribution("Why's it always the nice guys?")
Out : {'always': 1, 'it': 1, 'nice': 1, 'the': 1}

Example of what it should be producing.....

Out : {'always': 1, 'it': 1, 'nice': 1, 'the': 1, 'why's': 1, 'guys': 1}

Upvotes: 1

Views: 99

Answers (2)

Elidor00
Elidor00

Reputation: 1624

Hello and welcome on SO.

If I understand your problem, I think you simply wrote the construct if then else in the wrong way.

import string

def word_distribution(s):
    s = s.lower()
    s = s.split()
    wordfreq = {}
    for word in s:
        if word[-1] in string.punctuation:
            if word[-1] in wordfreq:
                wordfreq[word[:-1]] += 1 
            else:
                wordfreq[word[:-1]] = 1
        else:
            if word in wordfreq:
                wordfreq[word] += 1 
            else:
                wordfreq[word] = 1

    return wordfreq

x = word_distribution("Why's it always the nice guys?")
print(x) 

I used string.punctuation to check if the last character is a punctuation symbol.

The output is: {"why's": 1, 'it': 1, 'always': 1, 'the': 1, 'nice': 1, 'guys': 1}

Edit: Another solution using isalpha:

def word_distribution(s):
    s = s.lower()
    s = s.split()
    wordfreq = {}
    for word in s:
        if word[-1].isalpha():
            if word[-1] in wordfreq:
                wordfreq[word] += 1 
            else:
                wordfreq[word] = 1
        else:
            if word in wordfreq:
                wordfreq[word[:-1]] += 1 
            else:
                wordfreq[word[:-1]] = 1

    return wordfreq

x = word_distribution("Why's it always the nice guys?")
print(x)

The output is: {"why's": 1, 'it': 1, 'always': 1, 'the': 1, 'nice': 1, 'guys': 1}

Upvotes: 0

marcos
marcos

Reputation: 4510

Generally for counting stuff, you should use the collections.Counter class, and for checking if an element is a , or ? you should use string.punctuation that contains them, for example:

import string
from collections import Counter
txt = "Why's it always the nice guys?"

counted = Counter(
    word if not word[-1] in string.punctuation else word[:-1] for word in txt.split()
)
print(counted)

>>> Counter({"Why's": 1, 'it': 1, 'always': 1, 'the': 1, 'nice': 1, 'guys': 1})

Now if you really need a dictionary for output just do:

print(dict(counted))

>>> {"Why's": 1, 'it': 1, 'always': 1, 'the': 1, 'nice': 1, 'guys': 1}

Upvotes: 1

Related Questions