39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
# Title: Project 1 - Social Network Second Besties
|
|
# Author: Rory Healy
|
|
# Date created - 17th April 2019
|
|
# Date modified - 18th April 2019
|
|
|
|
def friend_second_besties(individual, bestie_dict):
|
|
'''Takes a person, stored as the string "individual", and the dictionary of
|
|
everyone's friends, stored as the dictionary "bestie_dict", and
|
|
returns a sorted list of the individual's degree-two friends.'''
|
|
|
|
# Creates a list of all people who are two degrees of seperation away from
|
|
# the individual.
|
|
second_best_friends = []
|
|
for item in list(bestie_dict.items()):
|
|
current_deg1_friend = item[0]
|
|
# Adds the degree-two friends to the list second_best_friends.
|
|
if current_deg1_friend == individual:
|
|
for current_deg2_friend in bestie_dict[current_deg1_friend]:
|
|
second_best_friends.append(bestie_dict[current_deg2_friend])
|
|
|
|
# Places the elements from the inner list of second_best_friends in the
|
|
# list second_best_friends and deletes the inner list.
|
|
return_list = []
|
|
if second_best_friends == return_list:
|
|
return_list = []
|
|
else:
|
|
for people in second_best_friends[0]:
|
|
return_list.append(people)
|
|
|
|
# Removes the individual and any immediate friends from the list
|
|
# return_list.
|
|
for deg2_friend in return_list:
|
|
if deg2_friend == individual:
|
|
return_list.remove(individual)
|
|
for deg2_friend in return_list:
|
|
if deg2_friend in bestie_dict[individual]:
|
|
return_list.remove(deg2_friend)
|
|
|
|
return sorted(return_list)
|