This commit is contained in:
blueloveTH 2024-04-14 00:11:11 +08:00
commit 7a145b6c02
8 changed files with 50 additions and 2 deletions

View File

@ -0,0 +1,7 @@
---
icon: package
label: itertools
---
### `itertools.zip_longest(a, b)`

File diff suppressed because one or more lines are too long

13
python/itertools.py Normal file
View File

@ -0,0 +1,13 @@
def zip_longest(a, b):
a = iter(a)
b = iter(b)
while True:
ai = next(a)
bi = next(b)
if ai is StopIteration and bi is StopIteration:
break
if ai is StopIteration:
ai = None
if bi is StopIteration:
bi = None
yield ai, bi

View File

@ -32,6 +32,8 @@ LiteralString = _PLACEHOLDER
Iterable = _PLACEHOLDER
Generator = _PLACEHOLDER
Hashable = _PLACEHOLDER
TypeVar = _PLACEHOLDER
Self = _PLACEHOLDER

View File

@ -1622,6 +1622,7 @@ void VM::post_init(){
_lazy_modules["typing"] = kPythonLibs_typing;
_lazy_modules["datetime"] = kPythonLibs_datetime;
_lazy_modules["cmath"] = kPythonLibs_cmath;
_lazy_modules["itertools"] = kPythonLibs_itertools;
try{
CodeObject_ code = compile(kPythonLibs_builtins, "<builtins>", EXEC_MODE);

View File

@ -729,7 +729,7 @@ void VM::init_builtin_types(){
this->Ellipsis = heap._new<Dummy>(_new_type_object("ellipsis"));
this->True = heap._new<Dummy>(tp_bool);
this->False = heap._new<Dummy>(tp_bool);
this->StopIteration = heap._new<Dummy>(_new_type_object("StopIterationType"));
this->StopIteration = _all_types[_new_type_object("StopIteration", tp_exception)].obj;
this->builtins = new_module("builtins");

View File

@ -36,3 +36,21 @@ assert next(i) == 4
assert next(i) == 5
assert next(i) == StopIteration
assert next(i) == StopIteration
import builtins
def next(obj):
res = builtins.next(obj)
if res is StopIteration:
raise StopIteration
return res
a = iter([1])
assert next(a) == 1
try:
next(a)
exit(1)
except StopIteration:
pass

6
tests/86_itertools.py Normal file
View File

@ -0,0 +1,6 @@
from itertools import zip_longest
a = [1, 2, 3]
b = [2]
assert list(zip_longest(a, b)) == [(1, 2), (2, None), (3, None)]