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

哈赞

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

[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])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章