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.
bigramdict = Counter(zip(temp_list, temp_list[1:]))
. (Not the most efficient way to do this, but the shortest and easiest to understand.) – Adaiha