Finding Mutual friends in Python

Lets take a List of Friends in Users as:

users=[
        {"id":0,"name": 'Saqib'},
        {"id":1,"name": 'Abid'},
        {"id":2,"name": 'Muza'},
        {"id":3,"name": 'Mustaq'},
        {"id":4,"name": 'Sai'},
        {"id":5,"name": 'Taha'},
        {"id":6,"name": 'Umair'}
        ]

We have 7 Names that have a unique ID as identifier. These can be friends with each other or may know some among themselves. Another List holds the relationship between these friends.

For example, the tuple (0, 1) indicates that with id 0 (Saqib) and with
id 1 (Abid) are friends.

friendship = [(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),
              (1,2),(1,3),(1,4),(1,5),
              (2,3),(2,4),(2,5),
              (3,4),(5,6)]

Lets create a new List as User[“Friends”]. Set this to null.

for user in users:
    user["friends"] = []

Lets add the Friendship based on the Friendship List

# Adding friendhsip bi-directionally   
for i, j in friendship:
    users[i]["friends"].append(users[j]['name']) # add i as a friend of j
    users[j]["friends"].append(users[i]['name']) # add j as a friend of i  

Build the Mutual Friend list between Name and Friend exactly as per the friendship list.
Function to return mutual friends between two friends

def mutual(user, other_user):
   return list(set(users[user]["friends"]).intersection(set(users[other_user]["friends"])))
friend_stat = []
i=0
for user in users:
    userID = user["id"]
    for friend in user["friends"]:
        # Get ID of the Friend
        
        otherID = next(index for (index, d) in enumerate(users) if d["name"] == friend)
        if mutual(userID,otherID):
            mutuals = mutual(userID,otherID)
            friend_stat.append({ "id": i, "Name": user["name"], "Friend":str(friend),"Mutual":mutuals , "Mutual_Friend_Count" : len(mutuals)})
            i+=1

print friend_stat

[{'Friend': 'Abid',
  'Mutual': ['Taha', 'Muza', 'Sai', 'Mustaq'],
  'Mutual_Friend_Count': 4,
  'Name': 'Saqib',
  'id': 0},
 {'Friend': 'Muza',
  'Mutual': ['Taha', 'Sai', 'Abid', 'Mustaq'],
  'Mutual_Friend_Count': 4,
  'Name': 'Saqib',
  'id': 1},
 {'Friend': 'Mustaq',
  'Mutual': ['Muza', 'Sai', 'Abid'],
  'Mutual_Friend_Count': 3,
  'Name': 'Saqib',
  'id': 2},
 {'Friend': 'Sai',
  'Mutual': ['Muza', 'Abid', 'Mustaq'],
  'Mutual_Friend_Count': 3,
  'Name': 'Saqib',
  'id': 3},
 {'Friend': 'Taha',
  'Mutual': ['Muza', 'Umair', 'Abid'],
  'Mutual_Friend_Count': 3,
  'Name': 'Saqib',
  'id': 4},
 {'Friend': 'Umair',
  'Mutual': ['Taha'],
  'Mutual_Friend_Count': 1,
  'Name': 'Saqib',
  'id': 5},
 {'Friend': 'Saqib',
  'Mutual': ['Taha', 'Muza', 'Sai', 'Mustaq'],
  'Mutual_Friend_Count': 4,
  'Name': 'Abid',
  'id': 6},
 {'Friend': 'Muza',
  'Mutual': ['Taha', 'Saqib', 'Sai', 'Mustaq'],
  'Mutual_Friend_Count': 4,
  'Name': 'Abid',
  'id': 7},
 {'Friend': 'Mustaq',
  'Mutual': ['Saqib', 'Sai', 'Muza'],
  'Mutual_Friend_Count': 3,
  'Name': 'Abid',
  'id': 8},
 {'Friend': 'Sai',
  'Mutual': ['Saqib', 'Muza', 'Mustaq'],
  'Mutual_Friend_Count': 3,
  'Name': 'Abid',
  'id': 9},
 {'Friend': 'Taha',
  'Mutual': ['Saqib', 'Muza'],
  'Mutual_Friend_Count': 2,
  'Name': 'Abid',
  'id': 10},
 {'Friend': 'Saqib',
  'Mutual': ['Taha', 'Sai', 'Abid', 'Mustaq'],
  'Mutual_Friend_Count': 4,
  'Name': 'Muza',
  'id': 11},
 {'Friend': 'Abid',
  'Mutual': ['Taha', 'Saqib', 'Sai', 'Mustaq'],
  'Mutual_Friend_Count': 4,
  'Name': 'Muza',
  'id': 12},
 {'Friend': 'Mustaq',
  'Mutual': ['Saqib', 'Sai', 'Abid'],
  'Mutual_Friend_Count': 3,
  'Name': 'Muza',
  'id': 13},
 {'Friend': 'Sai',
  'Mutual': ['Saqib', 'Abid', 'Mustaq'],
  'Mutual_Friend_Count': 3,
  'Name': 'Muza',
  'id': 14},
 {'Friend': 'Taha',
  'Mutual': ['Saqib', 'Abid'],
  'Mutual_Friend_Count': 2,
  'Name': 'Muza',
  'id': 15},
 {'Friend': 'Saqib',
  'Mutual': ['Muza', 'Sai', 'Abid'],
  'Mutual_Friend_Count': 3,
  'Name': 'Mustaq',
  'id': 16},
 {'Friend': 'Abid',
  'Mutual': ['Saqib', 'Sai', 'Muza'],
  'Mutual_Friend_Count': 3,
  'Name': 'Mustaq',
  'id': 17},
 {'Friend': 'Muza',
  'Mutual': ['Saqib', 'Sai', 'Abid'],
  'Mutual_Friend_Count': 3,
  'Name': 'Mustaq',
  'id': 18},
 {'Friend': 'Sai',
  'Mutual': ['Saqib', 'Abid', 'Muza'],
  'Mutual_Friend_Count': 3,
  'Name': 'Mustaq',
  'id': 19},
 {'Friend': 'Saqib',
  'Mutual': ['Muza', 'Abid', 'Mustaq'],
  'Mutual_Friend_Count': 3,
  'Name': 'Sai',
  'id': 20},
 {'Friend': 'Abid',
  'Mutual': ['Saqib', 'Muza', 'Mustaq'],
  'Mutual_Friend_Count': 3,
  'Name': 'Sai',
  'id': 21},
 {'Friend': 'Muza',
  'Mutual': ['Saqib', 'Abid', 'Mustaq'],
  'Mutual_Friend_Count': 3,
  'Name': 'Sai',
  'id': 22},
 {'Friend': 'Mustaq',
  'Mutual': ['Saqib', 'Abid', 'Muza'],
  'Mutual_Friend_Count': 3,
  'Name': 'Sai',
  'id': 23},
 {'Friend': 'Saqib',
  'Mutual': ['Muza', 'Umair', 'Abid'],
  'Mutual_Friend_Count': 3,
  'Name': 'Taha',
  'id': 24},
 {'Friend': 'Abid',
  'Mutual': ['Saqib', 'Muza'],
  'Mutual_Friend_Count': 2,
  'Name': 'Taha',
  'id': 25},
 {'Friend': 'Muza',
  'Mutual': ['Saqib', 'Abid'],
  'Mutual_Friend_Count': 2,
  'Name': 'Taha',
  'id': 26},
 {'Friend': 'Umair',
  'Mutual': ['Saqib'],
  'Mutual_Friend_Count': 1,
  'Name': 'Taha',
  'id': 27},
 {'Friend': 'Saqib',
  'Mutual': ['Taha'],
  'Mutual_Friend_Count': 1,
  'Name': 'Umair',
  'id': 28},
 {'Friend': 'Taha',
  'Mutual': ['Saqib'],
  'Mutual_Friend_Count': 1,
  'Name': 'Umair',
  'id': 29}]

Final Single Code

# Assuming a bunch of friends
users=[
        {"id":0,"name": 'Saqib'},
        {"id":1,"name": 'Abid'},
        {"id":2,"name": 'Muza'},
        {"id":3,"name": 'Mustaq'},
        {"id":4,"name": 'Sai'},
        {"id":5,"name": 'Taha'},
        {"id":6,"name": 'Umair'}
        ]

# Assuming some kind of chemistry
friendship = [(0,1),(0,2),(0,3),(0,4),(0,5),(0,6),
              (1,2),(1,3),(1,4),(1,5),
              (2,3),(2,4),(2,5),
              (3,4),(5,6)]

# Creating a new item in Users List
for user in users:
    user["friends"] = []

# Adding friendhsip bi-directionally   
for i, j in friendship:
    users[i]["friends"].append(users[j]['name']) # add i as a friend of j
    users[j]["friends"].append(users[i]['name']) # add j as a friend of i  

# Get intersections of two listsmutual friends between two friends
def mutual(user, other_user):
   return list(set(users[user]["friends"]).intersection(set(users[other_user]["friends"])))

# Construct Mutual Friend List from data.
friend_stat = []
i=0
for user in users:
    userID = user["id"]
    for friend in user["friends"]:
        # Get ID of the Friend
        otherID = next(index for (index, d) in enumerate(users) if d["name"] == friend)
        # get mutual friends between two friends
        if mutual(userID,otherID):
            mutuals = mutual(userID,otherID)
            friend_stat.append({
              "id": i, 
              "Name": user["name"],
              "Friend":str(friend),
              "Mutual":mutuals ,
              "Mutual_Friend_Count" : len(mutuals)
              })
            i+=1
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: