robbin
robbin

Reputation: 323

How to resolve this dictionary KeyError?

I am trying to compare string2 words in string1 and if they are found, I am trying to add the word and their frequency into a dictionary. but I received this error which means that key does not exist.

string1 = 'here i i go and will see see sun'
string2 = 'i will go'
found = {}
new_words = {}

for word in string2.split():
    if word in string1.split():
        found[word] += 1
    else:
        new_words[word] +=1

I got this error.

KeyError: 'i'

I am trying to get Expect output as: these are the words which consist of string2 and they are found in string1 and their frequency:

found {'i': 2, 'will': 1, 'go': 1}

new_words are those words which are not in string2 but they are in string1, so they are new_words.

new_words {'here': 1, 'see': 2, 'sun': 1, 'and':1}

I am newbie to programming.Can someone help me solve this simple problem? I cannot store keys before because I don't know which words will be there in strings and their frequency.

Upvotes: 2

Views: 1946

Answers (5)

Pedro Lobito
Pedro Lobito

Reputation: 99001

You get a KeyError because the key doesn't yet (found[word] += 1), you may want to use:

string1 = 'here i go and i will see sun and enjoy the sunny day'
string2 = 'i will not go'
found = {}
new_words = {}

for word in string1.split():
    if word in string2.split():
        if word in found:
            found[word] += 1
        else:
            found[word] = 1
    else:
        if word in new_words:
            new_words[word] +=1
        else:
            new_words[word] = 1

Upvotes: 0

kvorobiev
kvorobiev

Reputation: 5070

One more option - you could use defaultdict

from collections import defaultdict

string1 = 'here i go and i will see sun and enjoy the sunny day'
string2 = 'i will not go'
found = defaultdict(int)
new_words = defaultdict(int)
for word in string2.split():
    if word in string1.split():
        found[word] += 1
    else:
        new_words[word] +=1

UPDATE

To solve your second problem you should exchange string2 and string1 in for loop. You should iterate over all words in string1 and check them in string2.

from collections import defaultdict

string1 = 'here i go and i will see sun and enjoy the sunny day'
string2 = 'i will not go'
found = defaultdict(int)
new_words = defaultdict(int)

for word in string1.split():
    if word in string2.split():
        found[word] += 1
    else:
        new_words[word] +=1

found
Out[66]: defaultdict(int, {'go': 1, 'i': 2, 'will': 1})

new_words
Out[67]: 
defaultdict(int,
            {'and': 2,
             'day': 1,
             'enjoy': 1,
             'here': 1,
             'see': 1,
             'sun': 1,
             'sunny': 1,
             'the': 1})

Upvotes: 1

Custer Barnes
Custer Barnes

Reputation: 51

You cannot increment an integer value that does not exist yet, which is the case the first time the word is encountered.

for word in string2.split():
    if word in string1.split():
        if word not in found.keys():
            found[word] = 1
        else:
            found[word] += 1
    else:
        if word not in new_words.keys():
            new_words[word] = 1
        else:
            new_words[word] += 1

Upvotes: 1

Astrom
Astrom

Reputation: 767

You are trying to increment (+1) a key that does not exist in your dictionnary. You should check first if the key exist. with:

if key in dict:
     dict[key]+=1
else:
     dict[key]=1

Upvotes: 0

ospahiu
ospahiu

Reputation: 3525

You're trying to add to a value that may or may not exist in the dictionary. Use get() instead to ensure safe execution:

found[word] = found.get(word, default=0) + 1

and

new_words[word] = new_words.get(word, default=0) + 1

Upvotes: 1

Related Questions