[英]Where I Am Going Wrong(AttributeError)
我哪里错了
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 行, in runfile('C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py', wdir='C:/用户/苛刻/桌面/计算的乐趣 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(), filename, 'exec'), namespace)
文件“C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py”,第 87 行,在 final_points=keep_distribting(points,G)
文件“C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py”,第 52 行,在 keep_distribting new_points=distribute_points(G,points) #base价值
文件“C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py”,第 30 行,在distribute_points nodes=list(G.nodes()) # 获取节点列表并将其存储在节点变量中
AttributeError: 'list' object 没有属性 'nodes'
代码片段可以在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]))
问题在于这行代码:
final_points=keep_distribting(points,G)
对于 arguments, keep_distribting
的定义有不同的顺序。 它被定义为
def distribute_points(G,points):
所以在 function 内部, G
是以前的points
,反之亦然。
您可能需要考虑您的代码的一些其他问题:
keep_distribting
拼写错误,这使得遵循代码有点困难。G
被视为全局变量,而在其他函数中,它作为参数传递给 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.