1 回答
TA贡献1906条经验 获得超3个赞
你的 while 循环正在测试是否next_item
不为 null,意思是不等于0
、None
、[]
、{}
等...所以一旦该值取 0,循环就会停止。
相反,你可以这样做:
def test(current):
iterator = iter(current)
previous_item = next(iterator)
current_item = next(iterator)
next_item = 0
while next_item is not None:
try:
next_item = next(iterator)
except StopIteration:
next_item = None
if previous_item == current_item:
current_item = next_item
else:
yield previous_item
previous_item = current_item
current_item = next_item
if previous_item:
yield previous_item
添加回答
举报