I am trying to return list of (str, int)
tuple, which is the friend recommendations for the given person in a list of tuples where the first element of each tuple is a potential friend's name (in the same format as the dictionary keys) and the second element is that potential friend's score. Only potential friends with non-zero scores should be included in the list.
Here is an example of the format of the return value for this function:
[('Gloria Pritchett', 2),
('Manny Delgado', 1),
('Cameron Tucker', 1),
('Luke Dunphy', 3)]
For each person, all people in the social network who they are not currently friends with are potential friends. For a particular person, each potential friend is scored using the following point system: For every mutual friend that the person and the potential friend have, add 1 point to the potential friend's score For each network that the person and the potential friend both belong to, add 1 point to the potential friend's score If the person has the same last name as the potential friend, add 1 point to the potential friend's score, but only if they have something else in common (mutual friend(s), mutual network(s) or both). This is what I did:
and these are my two functions which works fine: first function returns dictionary of key:names value:friends' names second function returns dictionary of key:names value: networks
I get an error for def make_recommendations
. I don't have any clue what is the problem .. Please help me.
I'm not sure if this is doing what you think it's doing:
You can see what it's really doing by trying this:
This is effectively saying:
If you want to use the values in both lists, you should use
itertools.chain
:You make a similar error with:
(Note the typo in
freind
. This is probably giving you your error. Alsoprofiles_file
isn't defined anywhere in this function, is it in the global scope?) You probably mean:This is evaluated by Python as:
Also of note, in
person_to_friends
, you have:While this is technically correct, it's a lot more overhead (in comprehension as well as in processing) than the traditional:
I see that the problem is a bit complicated and contain many intersections, I suggest you simplify the solution and divide it to steps:
for example:
function to find the
score
of person's friends (return list of tuples):function to find the
score
of person's networks (return list of tuples):function to calculate the results of the previous functions for
all persons
(return dictionary:key
=name
,value
=list of tuples
):function to
merge
the final result:Outputs would look like:
hope this helps, if not to give you exactly what you asked for, something to guide you. Good luck.