我要去哪里错了
nodes=list(G.nodes())
完整的代码段中
def distribute_points(G,points): #takes two arguments
nodes=list(G.nodes()) # get list of nodes and stores it in nodes variable
new_points=[] #create a new points list/array
#giving part
for i in range(len(nodes)): #itterating over len of list
new_points.append(0) #intial points
# reciving part
for n in nodes: #iterating over nodes list
out=list(G.out_edges(n)) #getting list of nodes
if len (out)==0: #if a sink
new_points[n]=new_points[n]+points[n] #completely give the share
else:
share=points[n]/len(out) #share equally point in n to len of out list
for (src,tgt) in out: #giving target nodes the points in share of outgoing list
new_points[tgt]=new_points[tgt]+share #new points of target = target+share its reciving.if not done previous value will change and we need to retain it
return new_points #return new points
我在这里称它为:
def keep_distribting(G,points):
while (1):
new_points=**distribute_points(G,points)** #base value
print(new_points)
points=new_points #new points will be old points at base for n no of itteration
stop=input("Press 0 To Stop Or Any Key To Continue: ") #user input to stop by 0
if stop=='0':
break
return new_point
因为有一个错误提示,我将它与相同的程序相匹配
我的错误弹出为:
回溯(最近通话最近一次):
文件“”,第1行,在 runfile('C:/ Users / Harsh / Desktop / Joy Of Computing NPTEL /第12周(Google的工作方式)/点数分配方法(页面排名).py', wdir ='C:/ Users / Harsh / Desktop / Joy Of Computing NPTEL /第12周(如何使用Google 作品)')
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”, 运行文件中的第786行 execfile(文件名,命名空间)
文件 “ C:\ ProgramData \ Anaconda3 \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”, 第110行,在execfile中 exec(compile(f.read(),文件名,'exec'),命名空间)
文件“ C:/ Users / Harsh / Desktop / Joy Of Computing NPTEL /第12周(如何 Google Works)/点数分配方法(页面排名).py”,第87行,在 final_points = keep_distribting(points,G)
文件“ C:/ Users / Harsh / Desktop / Joy Of Computing NPTEL /第12周(如何 Google Works)/点数分配方法(页面排名).py”,第52行,在 keep_distribting new_points = distribute_points(G,points)#基本值
文件“ C:/ Users / Harsh / Desktop / Joy Of Computing NPTEL /第12周(如何 Google Works)/点数分配方法(页面排名).py”,第30行,在 Distribution_points nodes = list(G.nodes())#获取节点列表并将其存储在nodes变量中
AttributeError:“列表”对象没有属性“节点”
该代码段位于Page Rank
主要代码:
import networkx as nx
import matplotlib.pyplot as plt
import random
def add_edges():
nodes=list(G.nodes())
for s in nodes:
for t in nodes:
if s!=t:
r=random.random()
if r<=0.5:
G.add_edge(s,t)
return G
def assign_points(G):
nodes=list(G.nodes())
p=[]
for each in nodes:
p.append(100)
return p
def distribute_points(G,points):
nodes=list(G.nodes())
new_points=[]
#giving part
for i in range(len(nodes)):
new_points.append(0)
for n in nodes:
out=list(G.out_edges(n))
if len (out)==0:
new_points[n]=new_points[n]+points[n]
else:
share=points[n]/len(out)
for (src,tgt) in out:
new_points[tgt]=new_points[tgt]+share
return new_points
def keep_distribting(G,points):
while (1):
new_points=distribute_points(G,points)
points=new_points
stop=input("Press 0 To Stop Or Any Key To Continue: ")
if stop=='0':
break
return new_points
def rank_by_point(final_points):
d={}
for i in range(len(points)):
d[i]=points[i]
sorted(d.items(),key=lambda f:f[1])
print(sorted(d.items(),key=lambda f:f[1]))
G=nx.DiGraph()
G.add_nodes_from([i for i in range(10)])
G=add_edges()
nx.draw(G,with_labels=True)
plt.show()
points=assign_points(G)
final_points=keep_distribting(points,G)
rank_by_point(final_points)
result=nx.pagerank(G)
print(sorted(result.items(),key=lambda f:f[1]))
答案 0 :(得分:1)
问题出在这行代码上:
final_points=keep_distribting(points,G)
keep_distribting
的定义对参数具有不同的顺序。定义为
def distribute_points(G,points):
因此在函数内部,G
是以前的points
,反之亦然。
您可能需要考虑的其他一些代码问题:
keep_distribting
的拼写错误,因此遵循代码有点挑战。G
被视为全局变量,而在其他函数中,它被作为参数传递给函数。