我看到这个问题有点复杂,包含很多交叉点,建议你把解决方案简化,分步:
例如:
-
查找好友score的函数(返回元组列表):
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
-
查找人的网络score的函数(返回元组列表):
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
-
函数为all persons计算前面函数的结果(返回字典: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
-
函数merge最终结果:
from collections import Counter
def mrg(ddict, ndict):
merged = Counter(ddict)
merged.update(ndict)
return merged
输出如下所示:
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)]
希望这会有所帮助,如果不能准确地为您提供您所要求的东西,那么可以为您提供一些指导。
祝你好运。