python代码运行不成功求助!!运行结果是None为什么啊!?
frommatplotlib.pyplotimportplotfromnetworkximportGraphdefcommunity_label_propagation(...
from matplotlib.pyplot import plot
from networkx import Graph
def community_label_propagation(self, weights=None, initial=None, fixed=None):
if isinstance(fixed, basestring):
fixed = [bool(o) for o in g.vs[fixed]]
cl = GraphBase.community_label_propagation(self, weights, initial, fixed)
return VertexClustering(self, cl, modularity_params=dict(weights=weights))
f = open('yourtest.txt','r')
test = {}
for i in f.readlines()[1:]:
people,friends = i.split()[0],i.split()[1:] #根据自己数据集去索引相应内容
test.setdefault(people,friends)
g = Graph()
for i in test.keys():
g.add_node(str(i))
edges = []
for i in test.keys():
for j in test[i]:
edges.append((str(i),str(j)))
#去重
new = []
for i in edges:
new.append(tuple(sorted(list(i))))
g.add_edges_from(set(new))
print(g.community_label_propagation())
plot(g)
(最后运行出来的结果为啥是None啊??我知道是没有返回值的意思,可是应该怎么改呀,开头定义函数的部分不是已经有返回值了吗?求助大神该怎么整啊哭TAT 展开
from networkx import Graph
def community_label_propagation(self, weights=None, initial=None, fixed=None):
if isinstance(fixed, basestring):
fixed = [bool(o) for o in g.vs[fixed]]
cl = GraphBase.community_label_propagation(self, weights, initial, fixed)
return VertexClustering(self, cl, modularity_params=dict(weights=weights))
f = open('yourtest.txt','r')
test = {}
for i in f.readlines()[1:]:
people,friends = i.split()[0],i.split()[1:] #根据自己数据集去索引相应内容
test.setdefault(people,friends)
g = Graph()
for i in test.keys():
g.add_node(str(i))
edges = []
for i in test.keys():
for j in test[i]:
edges.append((str(i),str(j)))
#去重
new = []
for i in edges:
new.append(tuple(sorted(list(i))))
g.add_edges_from(set(new))
print(g.community_label_propagation())
plot(g)
(最后运行出来的结果为啥是None啊??我知道是没有返回值的意思,可是应该怎么改呀,开头定义函数的部分不是已经有返回值了吗?求助大神该怎么整啊哭TAT 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询