如果我有这样的元组列表。
friendface = [('zeus','apollo'),('zeus','aphrodite'), ('apollo','aphrodite'), ('athena','hera'), ('hera','aphrodite'), ('aphrodite','apollo'), ('aphrodite','zeus'), ('athena','aphrodite'), ('aphrodite','athena'), ('zeus','athena'), ('zeus','hera')
我想编写一个名为likes_relation(friendface)的函数,它返回一个字典,显示每个人与谁联系,解决方案应该看起来像这样..
>>> likes_relation(friendface)
{'Aphrodite': ['Apollo', 'Zeus', 'Athena'],
'Hera': ['Aphrodite'],
'Zeus': ['Apollo', 'Aphrodite', 'Athena', 'Hera'],
'Apollo': ['Aphrodite'],
'Athena': ['Hera', 'Aphrodite'] }
如果有人知道如何做到这一点,我将不胜感激,因为它现在真的开始让我烦恼了。
谢谢!
编辑:我目前有一些糟糕的代码。
def likes_relation(friendface):
dict_friends = dict(friendface)
for name in friendface:
if name not in dict(friendface):
#dict(friendface)[name[:] = name[1]
#What i'm trying to do is go and run through the list
# again and if one of the sets isn't in the dictionary
# then add it.... obviously i don't know how...
返回字典(朋友脸)