python list of (str,int) tuple dictionaries [close

2019-08-30 07:06发布

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.

2条回答
再贱就再见
2楼-- · 2019-08-30 07:26

I'm not sure if this is doing what you think it's doing:

for key in person_to_friends or person_to_networks:

You can see what it's really doing by trying this:

for x in [1,2,3] or [4,5,6]:
    print x

This is effectively saying:

for value in (first list if it's not empty otherwise second list)

If you want to use the values in both lists, you should use itertools.chain:

import itertools
for x in itertools.chain([1,2,3], [4,5,6]):
    print x

You make a similar error with:

if freind in person_to_friends[profiles_file] or person_to_networks[profiles_file]:

(Note the typo in freind. This is probably giving you your error. Also profiles_file isn't defined anywhere in this function, is it in the global scope?) You probably mean:

if friend in person_to_friends[profiles_file] or friend in person_to_networks[profiles_file]:

This is evaluated by Python as:

if (value in first sequence) OR (second sequence is not empty)

Also of note, in person_to_friends, you have:

name.update({lst[0]:lst[1:]})

While this is technically correct, it's a lot more overhead (in comprehension as well as in processing) than the traditional:

name[lst[0]] = lst[1:]
查看更多
冷血范
3楼-- · 2019-08-30 07:42

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:

  1. function to find the score of person's friends (return list of tuples):

    def friendsMu(person):
        result = []
        friends = person_to_friends[person]
        for key in person_to_friends:
            if key != person:
                friends2 = person_to_friends[key]
                intersect = list(set(friends) & set(friends2))
                p = len(intersect)
                if p != 0:
                    t = (key, p)
                    result.append(t)
        return result
    
  2. function to find the score of person's networks (return list of tuples):

    def networkMu(person):
        result = []
        friends = person_to_networks[person]
        for key in person_to_networks:
            if key != person:
                friends2 = person_to_networks[key]
                intersect = list(set(friends) & set(friends2))
                p = len(intersect)
                if p != 0:
                    t = (key, p)
                    result.append(t)
        return result
    
  3. function to calculate the results of the previous functions for all persons (return dictionary: key = name, value = list of tuples):

    def allf();
        ddict = {}
        for key in person_to_friends:
            d1 = friendsMu(key)
            if d1 != ():
                ddict[key] = d1
        return ddict
    
    def alln():
        ndict = {}
        for key in person_to_networks:
            d1 = networkMu(key)
            if d1 != ():
                ndict[key] = d1
        return ndict
    
  4. function to merge the final result:

    from collections import Counter
    def mrg(ddict, ndict):
        merged = Counter(ddict)
        merged.update(ndict)
        return merged
    

Outputs would look like:

 print allf()
 allf() =  {'Jay Pritchett': [('Manny Delgado', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1), ('Luke Dunphy', 1)], 'Claire Dunphy': [('Gloria Pritchett', 1), ('Luke Dunphy', 1)], 'Manny Delgado': [('Jay Pritchett', 1), ('Mitchell Pritchett', 1), ('Alex Dunphy', 1), ('Cameron Tucker', 1)], 'Mitchell Pritchett': [('Manny Delgado', 1), ('Phil Dunphy', 1), ('Alex Dunphy', 1)], 'Alex Dunphy': [('Manny Delgado', 1), ('Mitchell Pritchett', 1)], 'Cameron Tucker': [('Jay Pritchett', 1), ('Manny Delgado', 1), ('Luke Dunphy', 1)], 'Haley Gwendolyn Dunphy': [], 'Phil Dunphy': [('Mitchell Pritchett', 1)], 'Dylan D-Money': [], 'Gloria Pritchett': [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Luke Dunphy', 1)], 'Luke Dunphy': [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1)]}

 print alln()
 alln() =  {'Phil Dunphy': [], 'Claire Dunphy': [('Gloria Pritchett', 1)], 'Manny Delgado': [('Alex Dunphy', 1)], 'Mitchell Pritchett': [], 'Alex Dunphy': [('Manny Delgado', 1)], 'Cameron Tucker': [], 'Gloria Pritchett': [('Claire Dunphy', 1)]}

merged = mrg(allf(),alln())
for i in merged:                                                                 
    print i, merged[i]
merged = 
Jay Pritchett [('Manny Delgado', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1), ('Luke Dunphy', 1)]
Claire Dunphy [('Gloria Pritchett', 1), ('Luke Dunphy', 1), ('Gloria Pritchett', 1)]
Manny Delgado [('Jay Pritchett', 1), ('Mitchell Pritchett', 1), ('Alex Dunphy', 1), ('Cameron Tucker', 1), ('Alex Dunphy', 1)]
Mitchell Pritchett [('Manny Delgado', 1), ('Phil Dunphy', 1), ('Alex Dunphy', 1)]
Alex Dunphy [('Manny Delgado', 1), ('Mitchell Pritchett', 1), ('Manny Delgado', 1)]
Cameron Tucker [('Jay Pritchett', 1), ('Manny Delgado', 1), ('Luke Dunphy', 1)]
Haley Gwendolyn Dunphy []
Phil Dunphy [('Mitchell Pritchett', 1)]
Dylan D-Money []
Gloria Pritchett [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Luke Dunphy', 1), ('Claire Dunphy', 1)]
Luke Dunphy [('Jay Pritchett', 1), ('Claire Dunphy', 1), ('Cameron Tucker', 1), ('Gloria Pritchett', 1)]

hope this helps, if not to give you exactly what you asked for, something to guide you. Good luck.

查看更多
登录 后发表回答