0
  1. StopIteration运行下面的代码时出现错误。我看到其他一些关于堆栈溢出的帖子也出现了同样的错误,但不确定它们是否适合这种情况。为什么会发生这种情况,我该如何解决?目标是将break数据框列升级为n = 15,000.

  2. 我也知道最后一行是多余的。我在 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: 
4

1 回答 1

1

该异常与数据帧无关。如果您尝试从任何已经结束生命周期的迭代器中提取更多数据,就会发生这种情况:

>>> def f():
...     yield 1
...     yield 2
>>> output = f()
>>> next(output)
1
>>> next(output)
2
>>> next(output)
Traceback (most recent call last):
...
StopIteration
>>> 
于 2022-01-17T00:55:28.370 回答