2012-06-25 25 views
-1

我有文件名列表通过processTestFile()函数,返回结果的字典,我想存储运行:如何在for循环中组织函数结果?

# Count success or failure of test cases 
    counts = { 
    "success": 0, 
    "failure": 0 
    } 

主要工艺循环如下所示:

for fileName in all_files(fileReferences["testCasesPath"],"*.txt") : 
    print "\n\nRunning test case file = ", fileName 
    myCounts = processTestFile(fileName) 

我想的是一样的东西:

myArray = {} 
    myArray[fileName] = myCounts 

,我会作为参考:

for name in myArray 
    print name, myArray[name] ["success"], myArray[name] ["failure"] 

我怀疑这个代码会起作用。你有什么建议?

+4

什么是真正的问题? – Kugel

+0

为什么你怀疑它会起作用?它失败了吗?什么是错误? – jdi

+1

请阅读[PEP 8 - Python代码风格指南](http://www.python.org/dev/peps/pep-0008/)! – astynax

回答

1

myArraydict的奇怪名称。确保你初始化它

myArray = {} 
for fileName in all_files(fileReferences["testCasesPath"], "*.txt"): 
    print "\n\nRunning test case file = ", fileName 
    myCounts = processTestFile(fileName) 
    myArray[fileName] = myCounts 

for name, value in myArray.items() 
    print name, value["success"], value["failure"] 
+0

Q的好解释。为@historystamp添加,你可以将processTestFile()结果直接分配给myArray字典,并取消myCounts变量。 – invert

+0

感谢gnibbler。 wez的好评。我正在寻找实现此目的的最佳方式。这看起来不错。我想,因为你用[]访问这些集合,我认为它们是数组。 – historystamp

+0

'对于myArray.items()中的名称,值:'#注意尾部冒号: – historystamp