如何将列表添加到字典的值中?

How to add list to values of Dictionary?

我正在编写一个检测文件方法的脚本。 键是函数名称,值以这种格式保存其路径、行、列:

[path,row,column]

相同的功能可能出现在不同的位置(相同的文件或不同的文件)

所以我正在检查这种情况:如果我们有相同的功能,那么我们添加与其键对应的新值。

if methodDictionary.get(functionName) is None:
    methodDictionary[functionName] =[path,row,column]
elif [path,row,column]  not in methodDictionary[functionName]:                                                                                                                                                             
    methodDictionary[functionName] =[methodDictionary.get(functionName)]                                                     
    methodDictionary[functionName].append([path,row,column])

我认为问题出在elif 语句中。它无法检查键是否具有相同的值。但我无法解决这个问题。从输出中也可以看出格式问题。

我想要这样的格式;

{'int a(int x)' : [[path1,row1,column1],[path2,row2,column2]]}

我该怎么做?

代码输出:

f()': [[[[[[[[[[[[[[[[[['a.cpp', '3', '3'], ['a.cpp', '8', '3']], ['a.cpp', '13', '3']], ['a.cpp', '3', '3']], ['a.cpp', '8', '3']], ['a.cpp', '13', '3']], ['a.cpp', '3', '3']], ['a.cpp', '8', '3']], ['a.cpp', '13', '3']], ['a.cpp', '3', '3']], ['a.cpp', '8', '3']], ['a.cpp', '13', '3']], ['a.cpp', '3', '3']], ['a.cpp', '8', '3']], ['a.cpp', '13', '3']], ['a.cpp', '3', '3']], ['a.cpp', '8', '3']], ['a.cpp', '13', '3']]

根据代码,问题可能出在这里:

methodDictionary[functionName] =[methodDictionary.get(functionName)]                                                     

每次执行 elif 块时,您都在更新密钥。而是尝试做:

if methodDictionary.get(functionName) is None:
    methodDictionary[functionName] =[[path,row,column]]
else:                                                    
    methodDictionary[functionName].append([path,row,column])