Python List Of (str,int) Tuple Dictionaries
Solution 1:
I'm not sure if this is doing what you think it's doing:
forkeyin 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:
iffriend in person_to_friends[profiles_file] orfriend 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:
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) returnresult
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) returnresult
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
function to
merge
the final result:from collections import Counter defmrg(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"