簡體   English   中英

我哪里錯了(AttributeError)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM