7

Imagine we have a piece of code which cuts the large data into smaller data and do 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 return the first data list, the for loop stop going on for the rest of the large data. How I can make sure that it returns but still the loop continues?

2 Answers 2

7

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for loop was a great advice!
5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.