如何在for循环中组织函数结果?

时间:2012-06-25 00:16:23

标签: python for-loop

我有一个文件名列表要通过processTestFile()函数运行,该函数返回我想要存储的结果字典:

 # 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"]

我怀疑这段代码是否有用。你有什么建议吗?

1 个答案:

答案 0 :(得分: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"]