我想使用父类“Graph”中的字典并在名为“Summary”的子类中使用它。
字典是节点、度数、邻居、直方图和图。
尝试使用“graph.(tab completion)”时无法使用摘要中的函数,这就是我遇到的问题。
我是python和一般编程的新手,所以我不知道我想做的事情是否可行。
class Graph:
'''docstring'''
def __init__(self, graph):
d = {}
d1 = {}
d2 ={}
with myfile as f:
next(f)
for line in f:
k, v = line.split()
d[int(k)] = int(v)
next(f)
myfile.seek(0)
data = [line.strip() for line in myfile]
d1 = dict(enumerate(data))
del d1[0]
d2 = {key: list(map(int, value.split())) for key, value in d1.items()}
i = 1
while i <= max(d2.keys()):
del d2[i]
i += 2
neighbors = dict(enumerate(d2.values(), start = 1))
hist = defaultdict(list)
for key, values in neighbors.iteritems():
hist[len(values)].append(key)
histogram = dict(hist)
degree = d.values()
nodes = d.keys()
self.graph = graph
self.nodes = nodes
self.degree = degree
self.neighbors = neighbors
self.histogram = histogram
class Summary(Graph):
def __init__(self, graph):
Graph.__init__(self, graph)
def Avg_Connectivity(self):
return ("Average Node Connectivity:", np.average(self.degree))
def Max_Connectivity(self):
return ("Maximum Node Connectivity:" , max(self.degree)),("Node with Maximum Connectivity:", self.nodes[self.degree.index(max(self.degree))])
def Min_Connectivity(self):
return ("Minimum Node Connectivity:", min(x for x in self.histogram.keys() if x != 0)),("Nodes with Minimum Connectivity", self.histogram[min(x for x in self.histogram.keys() if x != 0)])
if __name__ == '__main__':
import numpy as np
from collections import defaultdict
infile = raw_input("Enter File Name (e.g. e06.gal): ")
myfile = open(infile, 'r')
graph = Graph(myfile)