1

So I am using classes and def to make it so if 2 People (Which I have as a class) are in each others friends list, well their ID which is specific to each person. Be removed from that list, and be added to a mutual friends lists.

I haven't tried much, I am stuck on comparing those 2 lists.

class People:

    '''People to make friendships, have a name, and a unique ID'''

    numsTimes = 0           ###Class variable for ID

    def __init__(self, name="anon"):
        if name == "anon": self.myname = makeRName()   ####Random Name
        else: self.myname = name
        self.friends = [] 
        self.mutualf = [] 

        self.ID = People.numsTimes          ###Unique ID
        People.numsTimes += 1

    def addFriend(self):            ###Ability for people to add others as friends
        self.friends.append(People.ID)

    def addMutual(self):
        ################I am looking for some if statement here.
        ###############Somehow remove others ID from both lists        
        self.mutualf.append(People.ID)
        else: return

I hope that it will check eachothers friends list, if they are friends with eachother, they will be added to eachothers mutual list and removed from friends list.

Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40

1 Answers1

2

If I were you, I would use sets instead of lists for friends. You could compare mutual friends with:

class People:

    '''People to make friendships, have a name, and a unique ID'''

    numsTimes = 0           ###Class variable for ID

    def __init__(self, name="anon"):
        if name == "anon": self.myname = makeRName()   ####Random Name
        else: self.myname = name
        self.friends = {}
        self.mutualf = {} 

        self.ID = People.numsTimes          ###Unique ID
        People.numsTimes += 1

    def addMutual(self,other):
        mutual = self.friends.intersection(other.friends)
        self.mutual.add(mutual)
        self.friends.remove(mutual)
Nakor
  • 1,484
  • 2
  • 13
  • 23