How to continue after return command in python?

Imagine that we have a piece of code that shrinks big data into smaller data and performs some process on it.

def node_cut(input_file): NODE_LENGTH = 500 count_output = 0 node_list=[] for line in input_file.readlines(): if len(node_list) >= NODE_LENGTH : count_output += 1 return( node_list,count_output ) node_list=[] node,t=line.split(',') node_list.append(node) if __name__ =='__main__': input_data = open('all_nodes.txt','r') node_list, count_output = node_cut(input_data) some_process(node_list) 

while node_cut returns the first list of data, the for loop stops for the rest of the big data. How can I make sure it returns, but the cycle continues?

+4
source share
2 answers

Use yield :

 def node_cut(input_file): NODE_LENGTH = 500 count_output = 0 node_list=[] for line in input_file.readlines(): if len(node_list) >= NODE_LENGTH : count_output += 1 yield( node_list,count_output ) node_list=[] node,t=line.split(',') node_list.append(node) if __name__ =='__main__': with open('all_nodes.txt','r') as input_data: for node_list, count_output in node_cut(input_data): some_process(node_list) 
+3
source

Use yield instead of return . See this question or this (somewhat old) article on how it works.

+3
source

All Articles