简体   繁体   中英

Where I Am Going Wrong(AttributeError)

Where am I going wrong a

nodes=list(G.nodes())

in whole snippet of code in

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

And I called it here as:

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

as there is an error saying and I matched it with the same program its same

My error popes up as:

Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py', wdir='C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)')

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile execfile(filename, namespace)

File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py", line 87, in final_points=keep_distribting(points,G)

File "C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py", line 52, in keep_distribting new_points=distribute_points(G,points) #base value

File "C:/Users/Harsh/Desktop/Joy Of Computing NPTEL/Week 12(How Google Works)/Points Distribution Method(Page Rank).py", line 30, in distribute_points nodes=list(G.nodes()) # get list of nodes and stores it in nodes variable

AttributeError: 'list' object has no attribute 'nodes'

The code snippet can be found at Page Rank

Main code:

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]))

The problem is with this line of code:

final_points=keep_distribting(points,G)

The definition of keep_distribting has a different order for the arguments. It is defined as

def distribute_points(G,points):

So inside the function, G is what used to be points and vice versa.


There are some additional issues with your code that you might want to think about:

  • keep_distribting is miss-spelled, which makes it a bit challenging to follow the code.
  • In some of your functions, G is treated as a global variable, while in others it's passed into the function as an argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM