Skip to content Skip to sidebar Skip to footer

Python List Of (str,int) Tuple Dictionaries

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 f

Solution 1:

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:]

Solution 2:

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.


Post a Comment for "Python List Of (str,int) Tuple Dictionaries"