zoukankan      html  css  js  c++  java
  • Python 自定义iterator生成器

    #计数版
    class countdown(object):
    	def __init__(self,start):
    		self.start = start
    	def __iter__(self):
    		return countdown_iter(self.start)
    
    class countdown_iter(object):
    	def __init__(self, count):
    		self.count = count
    	def __next__(self):
    		if self.count <= 0:
    			raise StopIteration
    		r = self.count
    		self.count -= 1
    		return r
    

      

    #pandas读取多个文件
    import pandas as pd
    
    class read_csv_paths(object):
    	def __init__(self,paths):
    		self.paths = paths
    	def __iter__(self):
    		return read_csv_iter(self.paths)
    
    class read_csv_iter(object):
    	def __init__(self, paths):
    		self.paths = paths
    	def __next__(self):
    		if len(self.paths)<=0:
    			raise StopIteration
    		path=self.paths.pop()
    		#print(path)
    		df=pd.read_csv(path,low_memory=False,dtype='object')
    		return df
    
    #generator版
    def countdown(n):
    	print("Counting down from", n)
    	while n > 0:
    		yield n
    		n -= 1
    

      

      

  • 相关阅读:
    取消浏览器默认行为
    BootStrap基础
    JavaScript基础
    HTML和CSS基础
    03JDBC
    MySQL
    NER-BiLSTM+CRF
    pytroch-Dataset/DataLoader/collate_fn/pad_sequence方法介绍
    pytorch-LSTM()简单介绍
    NER-使用kashgari中的Bert+BiLSTM+CRF
  • 原文地址:https://www.cnblogs.com/mahailuo/p/10207620.html
Copyright © 2011-2022 走看看