StopIteration
运行下面的代码时出现错误。我看到其他一些关于堆栈溢出的帖子也出现了同样的错误,但不确定它们是否适合这种情况。为什么会发生这种情况,我该如何解决?目标是将break
数据框列升级为n = 15,000
.我也知道最后一行是多余的。我在 Stack Overflow 上读到了一些似乎暗示该
next()
部分需要处于 for 循环中的内容。这会解决StopIteration
问题吗?如果是这样,那 for 循环会是一个单独的函数,还是我可以把它放在下面的代码中?我尝试了后者,但没有成功。
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
outputs = chunks(df['Delay Comment'] ,15000)
c15k,c30k,c45k,c60k,c75k,c90k,c105k,c120k,c135k,c150k,c165k,c180k,c195k,c210k,c225k,c240k,c255k,c270k,c285k,c300k,c315k,c330k = next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs)
Error: ---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
C:\Users\HECTOR~1.HER\AppData\Local\Temp/ipykernel_9116/1126382813.py in <module>
5
6 outputs = chunks(df['Delay Comment'] ,15000)
----> 7 c15k,c30k,c45k,c60k,c75k,c90k,c105k,c120k,c135k,c150k,c165k,c180k,c195k,c210k,c225k,c240k,c255k,c270k,c285k,c300k,c315k,c330k = next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs), next(outputs)
StopIteration: