All I want if to increment value for a given key. For example, the string that appears representing g key ‘USA’ should have value updated. But I get a KeyError since it appears this is happening because the key cannot be incremented as shown below. Please kindly assist on how I can solve this?
import re
d = {}
def listdict(string):
pattern = "^(USA_239*)/?"
if re.match(pattern, string):
print("Found a match...")
key_id = pattern.strip("^(_239*)/?")
try:
#Getting error if I attempt to increment a key...
d[key_id] = d[key_id] + 1
except KeyError:
print("Key doesnt exist")
return d