为了更好地理解Python的生成器,我正在尝试在itertools模块中实现工具,并遇到麻烦izip:
def izip(*iterables):
its = tuple(iter(it) for it in iterables)
while True:
yield tuple(next(it) for it in its) # ERROR
# yield tuple(map(next, its)) # OK
Run Code Online (Sandbox Code Playgroud)
我的代码使用ERROR行,参考实现(在手册中给出)使用OK行,而不考虑其他微小差异.有了这个片段:
for x in izip([1, 2, 3], (4, 5)):
print x
Run Code Online (Sandbox Code Playgroud)
我的代码输出:
(1, 4)
(2, 5)
(3,)
()
()
... # indefinite ()
Run Code Online (Sandbox Code Playgroud)
,而预期的输出是:
(1, 4)
(2, 5)
Run Code Online (Sandbox Code Playgroud)
我的代码有什么问题,拜托?
我在Linux下测试这个小程序:
// foo.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int n = system(argv[1]);
printf("%d\n", n);
return n;
}
Run Code Online (Sandbox Code Playgroud)
无论输入命令行的是什么,echo $?总是打印0,例如:
$ ./foo anything
sh: anything: not found
32512
$ echo $?
0
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么不$?采取相同的价值n?我也测试了Win32下的程序,并echo %errorlevel%给出了相同的值n.谢谢!