[英]Python - index out of range
嘗試為 bash 腳本調整基本分析器,下面是代碼塊。
無法弄清楚為什么使用“if branch”分析代碼會拋出異常(例如,在使用重復文件運行此代碼時)。 根據我的追蹤,它在某處創建了一個額外的索引值。 如果這是微不足道的,因為我是 Python 的新手,我深表歉意,但是對於問題出在哪里或如何解決它的任何建議,我們將不勝感激
def collect_timings(profiled_line, i):
if i == len(results) - 1:
return [0] + profiled_line
timing = float(results[i+1][1].replace(".N", "")) - float(profiled_line[1].replace(".N", ""))
return [timing] + profiled_line
錯誤:
Traceback (most recent call last):
File "./profile", line 67, in <module>
main(sys.argv)
File "./profile", line 51, in main
profiling_time = map(collect_timings, results, range(len(results)))
File "./profile", line 24, in collect_timings
timing = float(results[i+1][1].replace(".N", "")) - float(profiled_line[1].replace(".N", ""))
IndexError: list index out of range
找到答案,張貼以防對某人有用。 問題在於輸出重定向:
echo "Found '$file' is a duplicate of '${filecksums[$cksum]}'" >&2
這作為一個單獨的條目傳遞給results
:
["Found 'txt2/0122file.bck' is a duplicate of 'txt2/0113file.txt'\n"]
不知道如何使代碼與重定向一起運行,但這不是必需的,所以我會避免它。
你說:
if i == len(results) - 1:
下面你說:
timing = float(results[i+1][1].replace(".N", ""))
所以你檢查“i”是否顯示結果數組的最后一個元素(即結果有 5 個從 0 到 4 的單元格,你檢查是否i == 4
(在這種情況下 len(results) == 5 AND NOT 4))然后你說results[i+1]
在上面的例子中等於results[5]
這是越界的。
也許你的意思是results[i]
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.