Python - 索引超出范围

标签 python python-2.7 profiler

尝试为 bash 脚本调整基本分析器,如下代码块。

无法弄清楚为什么使用“if 分支”分析代码会引发异常(例如,使用重复文件运行此 code 时)。根据我的追踪,它在某处创建了一个额外的索引值。如果这是微不足道的,我很抱歉,因为我是 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 

它作为一个单独的条目传递给结果:

["Found 'txt2/0122file.bck' is a duplicate of 'txt2/0113file.txt'\n"]

不知道如何使代码通过重定向运行,但这不是必需的,所以我会避免它。

关于Python - 索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59054450/

相关文章:

python - 使用 selenium 递归跟踪所有链接

python - 在python中通过正则表达式提取Alberta(加拿大)邮政编码

python - 迭代 python 集中的单个元素

c++ - 如何在 qt 5.3.0 中使用 qml 分析器?

netbeans - 无法启动Java ME项目的Netbeans 7.2 Profiler

python - 如何在Python中同时检查多行多列中的多个并发事件?

python - 在python中使用SSH

vim - 突出显示 python 文档字符串作为注释(vim 语法突出显示)

python - 如何在 Python 2.7 中查找仅在子类中定义的方法?

windows - 适用于 Windows 的 Profiler QT 应用程序