关于next() 函数的两段代码
关于Python中的next()函数;代码①:
listx = list(range(1,5))
listx = iter(listx)
while True:
x = next(listx,"a")
print(x)
if x == "a":
break
输出
1
2
3
4
a
这个输出内容可以理解,
代码②
listx = list(range(1,5))
listx = iter(listx)
for each in listx:
x = next(listx,"a")
print(x)
if x == "a":
break
输出
2
4
然后就结束了?
为什么会跳着输出2,4,同样都是next()函数,其他值为什么没有输出呢?
谢谢!
注意,第二个循环的头部是for each in listx:循环的头部也在调用 next
这个例子可以让你理解原因listx = list(range(1,5))
listx = iter(listx)
for each in listx:
x = next(listx,"a")
print(each,x)
if x == "a":
break输出1 2
3 4
页:
[1]