why am i getting a key error when trying to iterate through a list of key value pairs and count how many times the key occurs? the error i'm getting is something like KeyError: 3, which means the key doesn't exist. can i not add it like this? self.node_degree[source] += 1
class PageRank:
def __init__(self, edge_file):
self.node_degree = {}
self.max_node_id = 0
self.edge_file = edge_file
def read_edge_file(self, edge_file):
with open(edge_file) as f:
for line in f:
if line[0] != '%':
val = line.split()
yield int(val[0]), int(val[1])
def get_max_node_id(self):
return self.max_node_id
def calculate_node_degree(self):
for source,target in self.read_edge_file(self.edge_file):
self.node_degree[source] += 1