Solution:
When using next()
to access an iterator that has been iterated, there will be such an error: stopiteration
the solution is to give a default value: next (ITER, default)
, which will be output after the iteration is completed
suppose that the original writing method of the loop is:
a = next(iter_test) # Iteration completion will report an error StopIteration
print(a)
Change to:
a = next(iter_test,None)
if a is not None:
print(a)
Problem analysis
The following code will report an error:
iter_list = iter([1, 2, 3, 4])
for i in range(10):
print(next(iter_list))
Error will be reported after outputting 1234, which can be changed to:
iter_list = iter([1, 2, 3, 4])
for i in range(10):
a = next(iter_list, None)
if a is not None:
print(a)
This will output: 1 2 3 4
Read More:
- Uncaught Error: Error calling method on NPObject.
- Error lnk2038: detected “_ ITERATOR_ DEBUG_ Mismatched ‘level’ value of ‘0’
- Error occurred when finalizing generatordataset iterator [How to Solve]
- CUDA error: CUBLAS_STATUS_NOT_INITIALIZED when calling `cublasCreate(handle)`
- [Solved] RuntimeError: CUDA error: CUBLAS_STATUS_ALLOC_FAILED when calling `cublasCreate(handle)`
- [Solved] Internal error occurred: failed calling webhook “validate.nginx.ingress.kubernetes.io”:
- [AWS] Solve Error: An error occurred (AccessDenied) when calling the CreateMultipartUpload operation: Access Denied
- Calling Dubbo Service reported the following error (com.alibaba.dubbo.remoting.RemotingException) Causes and Solutions
- Error calling function %s at New Error
- [Solved] Kubernetes ingress-srv. error: failed calling webhook “validate.nginx.ingress.kubernetes.io”
- [Solved] Rancher Add User Error: x509: certificate has expired Internal error occurred: failed calling webhook “rancherauth.cattle.io”:
- Yaml is installed, and the error YAMLLoadWarning is reported as soon as the code runs: calling yaml.load() without Loader=… is deprecated, as the default Loader is unsafe.
- Aspecj cannot intercept method annotations on an interface
- How to Solve Excel Error Log 2: Sloving method
- RuntimeError: implement_array_function method already has a docstring(Pycharm install package error)
- Kafkaconsumer calls seek() method error [How to Solve]
- [Solved] Flink Error: Cannot resolve method addSource
- How to Solve skimage.metrics.compare_psnr Method Error
- VUE: Property or method “deleteFun“ is not defined on the instance but referenced during render. [How to Fix]
- [Solved] Mybatis Error: Resources.getResourceAsStream(resource) cannot find the method