Python內(nèi)置函數(shù) next的具體使用方法
Python 3中的File對象不支持next()方法。 Python 3有一個內(nèi)置函數(shù)next(),它通過調(diào)用其next ()方法從迭代器中檢索下一個項目。 如果給定了默認(rèn)值,則在迭代器耗盡返回此默認(rèn)值,否則會引發(fā)StopIteration。 該方法可用于從文件對象讀取下一個輸入行。
語法
以下是next()方法的語法 -
next(iterator[,default])
參數(shù)
- iterator − 要讀取行的文件對象
- default − 如果迭代器耗盡則返回此默認(rèn)值。 如果沒有給出此默認(rèn)值,則拋出 StopIteration 異常
返回值
此方法返回下一個輸入行
英文文檔:
next(iterator[, default])
Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
說明:
1. 函數(shù)必須接收一個可迭代對象參數(shù),每次調(diào)用的時候,返回可迭代對象的下一個元素。如果所有元素均已經(jīng)返回過,則拋出StopIteration 異常。
>>> a = iter('abcd')
>>> next(a)
'a'
>>> next(a)
'b'
>>> next(a)
'c'
>>> next(a)
'd'
>>> next(a)
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
next(a)
StopIteration
2. 函數(shù)可以接收一個可選的default參數(shù),傳入default參數(shù)后,如果可迭代對象還有元素沒有返回,則依次返回其元素值,如果所有元素已經(jīng)返回,則返回default指定的默認(rèn)值而不拋出StopIteration 異常。
>>> a = iter('abcd')
>>> next(a,'e')
'a'
>>> next(a,'e')
'b'
>>> next(a,'e')
'c'
>>> next(a,'e')
'd'
>>> next(a,'e')
'e'
>>> next(a,'e')
'e'
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python 內(nèi)置函數(shù)進制轉(zhuǎn)換的用法(十進制轉(zhuǎn)二進制、八進制、十六進制)
- python中的內(nèi)置函數(shù)getattr()介紹及示例
- Python常用內(nèi)置函數(shù)總結(jié)
- Python內(nèi)置函數(shù)bin() oct()等實現(xiàn)進制轉(zhuǎn)換
- Python內(nèi)置函數(shù)——__import__ 的使用方法
- Python內(nèi)置函數(shù)dir詳解
- Python內(nèi)置函數(shù)Type()函數(shù)一個有趣的用法
- python中的內(nèi)置函數(shù)max()和min()及mas()函數(shù)的高級用法
- Python標(biāo)準(zhǔn)庫內(nèi)置函數(shù)complex介紹
- Python內(nèi)置函數(shù)的用法實例教程
- Python max內(nèi)置函數(shù)詳細介紹
- Python內(nèi)置函數(shù)—vars的具體使用方法
- 深入理解Python3 內(nèi)置函數(shù)大全
- Python內(nèi)置函數(shù)reversed()用法分析
- Python學(xué)習(xí)教程之常用的內(nèi)置函數(shù)大全
- Python 內(nèi)置函數(shù)complex詳解
- Python兩個內(nèi)置函數(shù) locals 和globals(學(xué)習(xí)筆記)
- python中68個內(nèi)置函數(shù)的總結(jié)與介紹
相關(guān)文章
python encrypt 實現(xiàn)AES加密的實例詳解
在本篇文章里小編給大家分享的是關(guān)于python encrypt 實現(xiàn)AES加密的實例內(nèi)容,有興趣的朋友們可以參考下。2020-02-02
詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf
這篇文章主要介紹了詳解利用django中間件django.middleware.csrf.CsrfViewMiddleware防止csrf攻擊,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
PyInstaller將Python文件打包為exe后如何反編譯(破解源碼)以及防止反編譯
這篇文章主要介紹了PyInstaller將Python文件打包為exe后如何反編譯(破解源碼)以及防止反編譯,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

