马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
两种实现def zip_longest(*iterables, fillvalue=None):
iterators=tuple(iter(i) for i in iterables)
while True:
temp=[]
flag=True
for i in iterators:
try:
temp.append(next(i))
flag=False
except StopIteration:
temp.append(fillvalue)
if flag:
break
yield tuple(temp)
class zip_longest:
def __init__(self,*iterables,fillvalue=None):
self.__iterators=tuple(iter(i) for i in iterables)
self.__filevalue=fillvalue
def __iter__(self):
return self
def __next__(self):
temp=[]
flag=True
for i in self.__iterators:
try:
temp.append(next(i))
flag=False
except StopIteration:
temp.append(self.__filevalue)
if flag:
raise StopIteration
return tuple(temp)
如果代码有问题,欢迎在评论区留言指出! |