首先,我很抱歉让人困惑的标题,但让我们假设我们有一本字典:
dict = {'Parent': 'Grandparent', 'Daughter': 'Parent', 'Son': 'Parent'}
如何达到这个输出
new_dict = {'Parent': 'Grandparent', 'Daughter': ['Parent', 'Grandparent'], 'Son': ['Parent', 'Grandparent']}
我在想:
for key in dict:
for value in dict.values():
if key == value: #I didn't use 'in' because the string 'Parent' is part of 'Grandparent
#some action
谢谢你的时间
此解决方案也适用于您的情况
在我看来,这个解决方案很有吸引力:
请注意:不要给你的字典
dict
起名字,因为这是一个关键字,所以你可能会遇到麻烦相关问题 更多 >
编程相关推荐