自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python next函數(shù)實際操作教程

開發(fā) 后端
Python next函數(shù)在平時的使用中常見的一種函數(shù)表達式。但是在真正的使用中存在不少的問題,下面我們就來具體學習下相關(guān)的技術(shù)問題。

Python next函數(shù)在實際使用的時候有不少的問題需要我們學習。相關(guān)的技術(shù)需要不斷學習才能更好的掌握。下面就向大家介紹下有關(guān)于Python next函數(shù)的具體使用情況。

下面給出一個用iterator的實現(xiàn),一個CharBufReader類,封裝了buf,對外提供一次讀取一個byte的接口(內(nèi)部實現(xiàn)從buf讀取,buf讀完再fill buf)。這樣代碼好復(fù)用。

因為提供Python next函數(shù),所以可以用iterator訪問。但是效率上很慢,和以前不優(yōu)化,用file.read(1)差不多90s左右的時間??梢钥闯鼍褪侵饕且驗楹瘮?shù)調(diào)用造成了原來程序速度慢。而不是因為不用自己寫的緩沖讀文件時間長。

  1. class CharBufReader(object):  
  2. def __init__(self, mfile, bufSize = 1000):  
  3. self.mfile = mfile  
  4. #self.bufSize = 64 * 1024 #64k buf size  
  5. self.capacity = bufSize 
  6. self.buf = '' #buf of char  
  7. self.cur = len(self.buf)  
  8. self.size = len(self.buf)  
  9. def __iter__(self):  
  10. return self  
  11. def next(self):  
  12. if self.cur == self.size:  
  13. #if self.cur == len(self.buf):  
  14. #if self.cur == self.buf.__len__():  
  15. selfself.buf = self.mfile.read(self.capacity)  
  16. self.size = len(self.buf)  
  17. if self.size == 0:  
  18. raise StopIteration  
  19. self.cur = 0 
  20. self.cur += 1  
  21. return self.buf[self.cur - 1]   
  22. class Compressor():  
  23. def caculateFrequence(self):  
  24. """The first time of reading the input file and caculate each  
  25. character frequence store in self.dict  
  26. """  
  27. self.infile.seek(0)  
  28. reader = compressor.CharBufReader(self.infile)  
  29. for c in reader:  
  30. if c in self.dict:  
  31. self.dict[c] += 1  
  32. else:  
  33. self.dict[c] = 0 

以上就是對Python next函數(shù)的詳細介紹,希望大家有所收獲。

【編輯推薦】

  1. Python編程語言與Zpoe之間不解的情緣
  2. 簡讀靈活性的Python編程語言
  3. 對Python編程語言歷史說明介紹
  4. 有關(guān)Python編程語言進行描述
  5. Python編程語言與Java的性能比較
責任編輯:張浩 來源: CSDN
相關(guān)推薦

2020-04-28 15:10:12

OpenCV Pyth閾值Linux

2010-03-09 18:55:27

Python djan

2010-03-16 10:00:37

Python函數(shù)

2010-03-05 15:07:35

Python優(yōu)化圖片

2010-03-23 18:38:26

Python os.m

2010-03-09 09:32:20

Python網(wǎng)頁爬蟲

2010-03-12 15:29:19

Pythonexe

2010-05-10 10:19:28

Oracle實戰(zhàn)RMA

2010-03-16 12:39:09

python for

2010-03-25 17:28:41

Python配置

2010-06-01 15:54:46

MySQL-pytho

2010-05-19 10:37:06

MySQL expla

2010-04-01 14:06:13

Oracle Name

2010-04-20 11:06:33

Oracle索引

2010-03-23 17:24:08

Python遍歷目錄樹

2010-03-31 16:11:00

Oracle啟動

2010-04-01 13:39:43

Oracle Name

2010-05-18 17:39:13

MySQL alter

2010-04-14 17:06:41

Oracle安裝路徑

2010-04-16 13:59:40

Oracle數(shù)據(jù)
點贊
收藏

51CTO技術(shù)棧公眾號