在 For 循环中迭代以检查前一个值是否大于当前值

发布于 2025-01-14 19:29:16 字数 468 浏览 3 评论 0原文

pitch_per_ab = []
pitch_of_pa = pitcher['PitchofPA']

for i in range(len(pitch_of_pa)):
    prev_elem = pitch_of_pa[i-1]
    current_elem = pitch_of_pa[i]
    if current_elem <= prev_elem:
       np.append(pitch_per_ab, prev_elem)
    
pitch_per_ab

这是我当前的代码,当我运行它时,我得到一个 KeyError,只显示“-1”: 错误照片

理想情况下,如果pitch_of_pa中的先前值大于当前值,它将运行,而不是将先前的值附加到列表pitch_per_ab

pitch_per_ab = []
pitch_of_pa = pitcher['PitchofPA']

for i in range(len(pitch_of_pa)):
    prev_elem = pitch_of_pa[i-1]
    current_elem = pitch_of_pa[i]
    if current_elem <= prev_elem:
       np.append(pitch_per_ab, prev_elem)
    
pitch_per_ab

This is my current code, when I run it i get a KeyError that simply says "-1":
photo of error

Ideally, it would run and if the previous value in pitch_of_pa is greater than the current, than it will append the previous value to the list pitch_per_ab

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2025-01-21 19:29:17

发生这种情况的原因是:

prev_elem = pitch_of_pa[i-1]

i0 开始,执行此行时,值 -1 会导致错误:

KeyError: -1

This is happens due to:

prev_elem = pitch_of_pa[i-1]

i starts in 0 and when this row is executed, the value -1 causes the error:

KeyError: -1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文