我有以下问题,我不知道该怎么办。有人能帮忙吗?
“你是在开派对,想让志趣相投的人结对。 您已经收集了每位客人最感兴趣的活动的数据 像这样的字典:
interests = { 'Sophia' : 'Computer', 'Anders' : 'Dancing', 'Mette' : 'Dancing', 'Hikari' : 'Computer', 'Riko' : 'Rowing', 'Troels' : 'Rowing', 'Sarah' : 'Rowing' }
为了能够在聚会上快速提出主题,您需要转换此 将字典转换为不同的布局
您的任务是编写一个函数转换字典,将其作为参数 一个字典,如上面的一个,并返回一个字典,其中 每个键都是一个兴趣点,值是共享此项的所有人的列表 兴趣 例如,对于上面的列表,返回的字典是 (不考虑元素的特定顺序)
我需要打印:
{'Computer': ['Sophia', 'Hikari'], 'Dancing': ['Anders', 'Mette'], 'Rowing': ['Riko', 'Troels', 'Sarah']}
这就是我目前的情况:
interests = {
'Sophia' : 'Computer',
'Anders' : 'Dancing',
'Mette' : 'Dancing',
'Hikari' : 'Computer',
'Riko' : 'Rowing',
'Troels' : 'Rowing',
'Sarah' : 'Rowing'
}
# comvert dictionary into a different layout.
def transform_dictionary(interests):
new_dict = {}
key_list = []
for k, v in new_dict:
if value in new_dict:
new_dict[value] = k
return {}
print(transform_dictionary(interests))
这项工作:
您可以使用^{} ,然后只使用
append
,如下所示:输出:
你可以这样做:
输出:
相关问题 更多 >
编程相关推荐