31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
|
# Title: Project 1 - Friends
|
||
|
# Author: Rory Healy
|
||
|
# Date created - 16th April 2019
|
||
|
# Date modified - 18th April 2019
|
||
|
|
||
|
def get_friendly_dict(friend_list):
|
||
|
'''Takes a list of reciprocal friendships links between individuals,
|
||
|
friend_list, and calculates the degree-one friends of each individual.
|
||
|
Returns a dictionary of sets containing all the immediate friends.'''
|
||
|
|
||
|
# Creates a list of everyone.
|
||
|
everyone_list = []
|
||
|
for pairs in friend_list:
|
||
|
for person in pairs:
|
||
|
if person not in everyone_list:
|
||
|
everyone_list.append(person)
|
||
|
|
||
|
# Creates a dictionary using the people from everyone_list as keys, and
|
||
|
# assigns values as a set of immediate friends.
|
||
|
current_friends = []
|
||
|
friend_dict = {}
|
||
|
for person in everyone_list:
|
||
|
for pairs in friend_list:
|
||
|
# Adds the other person in the pair to the list current_friends.
|
||
|
if person in pairs:
|
||
|
current_friends.append(pairs[pairs.index(person) - 1])
|
||
|
friend_dict[person] = set(current_friends)
|
||
|
current_friends = []
|
||
|
|
||
|
return friend_dict
|