7

I have this problem:

I hav this code that is trying to count bigrams in a text file. An if statement checks wether the tuple is in a dictionary. If it is, the value (counter) is one-upped. If it doesn't exist, the code shoud create a key-value pair with the tuple as key and the value 1.

for i in range(len(temp_list)-1):
    temp_tuple=(temp_list[i], temp_list[i+1])
    if bigramdict[temp_tuple] in bigramdict:
        bigramdict[temp_tuple] = bigramdict[temp_tuple]+1
    else:
        bigramdict[temp_tuple] = 1

However, whenever I run the code, it throws a KeyError on the very first tuple. As far as I understand, KeyError gets thrown when a key in dict doesn't exist, which is the case here. That's why I have the if statement to see if there is a key. Normally, the program should see that there is no key and go to the else to create one.

However, it gets stuck on the if and complains about the missing key.

Why does it not recognize that this is a conditional statement?

Pls help.

PyDev
  • 91
  • 1
  • 5
  • Does this answer your question? [I'm getting Key error in python](https://stackoverflow.com/questions/10116518/im-getting-key-error-in-python) – Lazy Ren Sep 20 '20 at 18:50
  • `bigramdict = Counter(zip(temp_list, temp_list[1:]))`. (Not the most efficient way to do this, but the shortest and easiest to understand.) – chepner Sep 20 '20 at 20:03

2 Answers2

9

What you were trying to do was

if temp_tuple in bigramdict:

instead of

if bigramdict[temp_tuple] in bigramdict:
Captain Trojan
  • 2,800
  • 1
  • 11
  • 28
2

For a more Pythonic solution, you can pair adjacent items in temp_list by zipping it with itself but with an offset of 1, and use the dict.get method to default the value of a missing key to 0:

for temp_tuple in zip(temp_list, temp_list[1:]):
    bigramdict[temp_tuple] = bigramdict.get(temp_tuple, 0) + 1
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Instead of the default get, you could also use a defaultdict; `from collections import defaultdict // bigramdict = defaultdict(int) // bigramdict[temp_tuple] += 1`. – marcelm Sep 21 '20 at 07:10