📜  Python next() 方法

📅  最后修改于: 2022-05-13 01:54:43.264000             🧑  作者: Mango

Python next() 方法

Python next()函数返回迭代器的下一项。在本文中,我们将介绍next() 语法、next() 参数、next() 返回。

Python next() 方法示例

示例 1:演示 next() 的工作原理

在这里,我们将在循环中看到Python next()。

Python3
# Python code to demonstrate
# working of next()
 
# initializing list
list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print("The contents of list are : ")
 
# printing using next()
# using default
while (1):
    val = next(list1, 'end')
    if val == 'end':
        print('list end')
        break
    else:
        print(val)


Python3
list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print(list1)
print(next(list1))
print(next(list1))
print(next(list1))


Python3
list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print(list1)
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))


Python3
list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print(list1)
print(next(list1))
print(next(list1))
print(next(list1))
print(next(list1))
print(next(list1))
print(next(list1))


Python3
# Python code to demonstrate
# next() vs for loop
import time
 
# initializing list
list1 = [1, 2, 3, 4, 5]
 
# keeping list2
list2 = list1
 
# converting list to iterator
list1 = iter(list1)
 
print("The contents of list are : ")
 
# printing using next()
# using default
start_next = time.time()
while (1):
    val = next(list1, 'end')
    if val == 'end':
        break
    else:
        print(val)
print("Time taken for next() is : " + str(time.time() - start_next))
 
# printing using for loop
start_for = time.time()
for i in list2:
    print(i)
print("Time taken for loop is : " + str(time.time() - start_for))


输出:

The contents of list are : 
1
2
3
4
5
list end

示例 2:从迭代器中获取下一项

Python3

list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print(list1)
print(next(list1))
print(next(list1))
print(next(list1))

输出:


1
2
3

示例 3:将默认值传递给 next()

Python3

list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print(list1)
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))
print(next(list1, -1))

输出:


1
2
3
4
5
-1
-1

示例 6: Python next() 停止迭代

Python3

list1 = [1, 2, 3, 4, 5]
 
# converting list to iterator
list1 = iter(list1)
 
print(list1)
print(next(list1))
print(next(list1))
print(next(list1))
print(next(list1))
print(next(list1))
print(next(list1))

输出:


1
2
3
4
5
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
 in 
    10 print(next(list1))
    11 print(next(list1))
---> 12 print(next(list1))

StopIteration: 

当调用超出迭代器范围时,它会引发 Stopoteration 错误,为避免此错误,我们将使用默认值作为参数。

示例 5:性能分析

Python3

# Python code to demonstrate
# next() vs for loop
import time
 
# initializing list
list1 = [1, 2, 3, 4, 5]
 
# keeping list2
list2 = list1
 
# converting list to iterator
list1 = iter(list1)
 
print("The contents of list are : ")
 
# printing using next()
# using default
start_next = time.time()
while (1):
    val = next(list1, 'end')
    if val == 'end':
        break
    else:
        print(val)
print("Time taken for next() is : " + str(time.time() - start_next))
 
# printing using for loop
start_for = time.time()
for i in list2:
    print(i)
print("Time taken for loop is : " + str(time.time() - start_for))

输出:

The contents of list are : 
1
2
3
4
5
Time taken for next() is : 5.96046447754e-06
1
2
3
4
5
Time taken for loop is : 1.90734863281e-06

结果:在打印列表内容时,For 循环中的Python next是比 next() 更好的选择。

应用: next() 是打印 iter 类型容器组件的实用函数。它的用途是当容器的大小未知或者我们需要在列表/迭代器耗尽时给出提示。