0

Here is my problem,

from Link import Link

def generateCompartibleLinks(linksList):
    count =0
    updatedList =[]
    for i,item in enumerate(linksList):
        link = linksList[i];
        for j,item in enumerate(linksList):
            if not linksList[i].sharesNode(linksList[j]):
                link.addCompartibleLink(linksList[j])
        updatedList.append(link)
        print len(updatedList[i].compartibleLinks)
    return updatedList

link1 = Link("a","b",5)
link2 =Link("b","c",1)
link3 = Link("d","b",3)
link4 =Link("d","c",2)

list1 = [link2,link1,link3,link4]

list1 = generateCompartibleLinks(list1)

for link in list1:
    print str(len(link.compartibleLinks))+" : "+link.getLinkInfo()

I expect each of the links (link1,link2,link3, link4) to have a different set of compartibleLinks but once a change is made on any element in the list it's reflected on each of them which is not what I want. Here is class Link.

class Link(object):
    sendNode =None
    rcvNode =None
    timesToSchedule =1
    isScheduled = False
    compartibleLinks = []
    def __init__(self,sendNode,rcvNode,timesToschedule):
        self.sendNode=sendNode
        self.rcvNode = rcvNode
        self.timesToSchedule =timesToschedule

    def reduceTimeToSchedule(self):
        self.timesToSchedule -=1
        if  self.timesToSchedule<=0:
             self.timesToSchedule=0

    def needsToschedule(self):
        if  self.timesToSchedule==0:
            return False
        else:
            return True
    def addCompartibleLink(self,link):
        self.compartibleLinks.append(link)
    def isCompartible(self,link):
        return any(l == link for l in self.compartibleLinks);
        #if the link is not found in the set.
        return False;
    def sharesNode(self,link):
        return (self.sendNode==link.sendNode or self.rcvNode==link.rcvNode or self.rcvNode==link.sendNode or self.sendNode==link.rcvNode)
    def getLinkInfo(self):
        return 'sender: '+ self.sendNode+" , rec:"+self.rcvNode

I have tried to look at various solutions that seem related to my problem but nothing really works for me.

1 Answers1

-1

If I correctly understand what you're asking, then I suggest you use the built-in map function that is used to apply a given function to an iterable(i.e. your list). Of course you have to modify the generateCompatibleLinks function to accept a link and not a whole list.

kingJulian
  • 5,601
  • 5
  • 17
  • 30
  • Could I get an explanation as to why my answer was downvoted? Thank you in advance! – kingJulian Sep 07 '17 at 21:49
  • Thanks for your support. I was able to identify the problem. It was about intitializing the compartibleLinks list outside the constructor. I didn't down you :-). I appreciate your help. – Robert Kasumba Sep 07 '17 at 21:57