Saving an amount of found words

Desiigner Source

I've been doing this checkio tasks and I got stuck. I was given a list of some words and a string followed by a text. The task is I need to count the number of words from the list in the text and create a dictionary where the key is a word and value is amount. It must look like this:

popular_words('''

When I was One

I had just begun

When I was Two

I was nearly new

''', ['i', 'was', 'three', 'near'])

and the output:

{

'i': 4,

'was': 3,

'three': 0,

'near': 0

}

I did almost everything but I don't know how to add a word to this dictionary which is not in text (like 'near' : 3)

That's what I got:

result = {}
number = 0
list1 = []

words = '''

When I was One

I had just begun

When I was Two

I was nearly new

'''

check = ['i', 'was', 'three', 'near']
a = list(words.split())

for word in a:
    if word.lower() in check:
        wc = words.count(word)
        result[word] = wc


print(result)

I tried:

for word in a:
        if word.lower() in check:
            wc = words.count(word)
            if words.count(word) == 0:
                result[word] = 0
            else:
                result[word] = wc

But it doesn't work :(

pythonpython-3.x

Answers

answered 3 months ago Jean Rostan #1

Your code is not very efficient, as you're looping through the whole text, word by word, but then using .count(word) which also loops through the whole text. You might want to increment a counter (which happens to be your dictionary) like this:

if word.lower() in check:
    if word in result:
       word[result] += 1
    else:
       word[result] = 1

. After looping through your text, you can loop through your list of popular words and adding them to the dic if they're not in:

if not pop_word in result:
   result[pop] = 0

answered 3 months ago David L #2

first, word.split() already gives you a list. No need to convert it into a list again.

Second, what you should be doing is iterating through check (you only care about words in check), instead of through a.

Third, you could use dictionary comprehension to speed things up:

a = words.lower().split()
results = {word:a.count(word) for word in check}

going over the code, words.split() is the same as list(words.split()) The code for results in a pythonic (and faster) way of doing the following:

results = {}
for word in check:
    results[word] = a.count(word)

answered 3 months ago bphi #3

Try this, it should be faster than iterating yourself. The Counter class has the added benefit of handling words not found in the text.

from collections import Counter
result = {k: Counter(words.lower().split())[k] for k in check}

answered 3 months ago Elvir Muslic #4

from collections import Counter

words = '''

When I was One

I had just begun

When I was Two

I was nearly new

'''

check = ['i', 'was', 'three', 'near']

words = Counter(words.split())
newcounter = {}


for i in words:
    if i.lower() in check:
        newcounter[i.lower()] = words[i]


print(newcounter)

answered 3 months ago Serge Ballesta #5

You should initialize the result dictionary from the given list of interesting words, and then increase the count each time you find a word:

def popular_words(txt, wordlist):
    result = { w.lower():0 for w in wordlist }  # initialize count to 0
    for word in txt.split():
        w = word.lower()                        # only considere lower case
        if w in result:
            result[w] += 1                      # increase for each occurence
    return result

comments powered by Disqus