|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 永恒的蓝色梦想 于 2020-4-11 09:44 编辑
- class islice:
- def __init__(self,iterable,start=None,stop=None,step=None,/):
- if step is None:
- step=1
- elif not isinstance(step,int) or step<=0:
- raise ValueError("Step argument for islice() must be None or a positive integer")
- if stop is None:
- stop,start=start,stop
- if start is None:
- start=0
- elif not isinstance(start,int) or start<0:
- raise ValueError("Start argument for islice() must be None or a non-negative integer")
- if stop is None:
- self.__b=None
- elif isinstance(stop,int)and stop>=0:
- self.__b=((stop-start-1)//step+1) if start<stop else 0
- else:
- raise ValueError("Stop argument for islice() must be None or a non-negative integer")
-
- self.__c=step-1
- self.__a=start
- self.__it=iter(iterable)
- def __iter__(self):
- return self
- def __next__(self):
- if self.__b is None:
- pass
- elif self.__b:
- self.__b-=1
- else:
- raise StopIteration
- while self.__a:
- self.__it.__next__()
- self.__a-=1
- self.__a=self.__c
- return self.__it.__next__()
复制代码 如果代码有问题,欢迎在评论区指正! |
|