2012-06-25 28 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