我需要写一个函数,它接受一个字典,它的键等于一个名称,值就是字典。嵌套在其中的字典的键等于一个任务,其值等于该任务所用的小时数。我需要返回一个dictionary,其中task是键,value是值,name是键,value是小时。你知道吗
我不知道如何访问嵌套字典中的值,因此嵌套字典中的值与键完全相同。 以下是我所拥有的:
def sprintLog(sprnt):
new_dict = {}
new_dict = {x: {y: y for y in sprnt.keys() if x in sprnt[y]} for l in sprnt.values() for x in l}
return new_dict
如果我传入一本字典,例如:
d = {'Ben': {'task1': 5}, 'alex': {'task1': 10, 'task2': 4}}
我想买一本字典,比如:
new_dict = {'task1': {'Ben': 5, 'alex': 10}, 'task2': {'alex': 4}}
但我现在得到的是:
new_dict = {'task1': {'Ben': 'Ben', 'alex': 'alex'}, 'task2': {'alex': 'alex'}}
尽管上面的答案是正确的,但为了更好的理解,我还是将这张幻灯片滑动一下:)
我想这就是你想要的:
您需要使用
sprnt.items()
而不是sprnt.keys()
来获取值。你知道吗这是我的工作
相关问题 更多 >
编程相关推荐