3

I have this part of a code:

def readTXT():
    part_result = []
    '''Reading all data from text file'''
    with open('dataset/sometext.txt', 'r') as txt:
        for lines in txt:
            part = lines.split()
            part_result = [int(i) for i in part]
            #sorted([(p[0], p[14]) for p in part_result], key=lambda x: x[1])
            print(part_result)
            return part_result

And I'm trying to get all lists as a return, but for now I'll get only the first one, what is quite obvious, because my return is inside the for loop. But still, shouldn't the loop go through every line and return the corresponding list?

After doing research, all I found was return list1, list2 etc. But have should I manage it, if my lists will be generated from a text file line by line?

It frustates me, not being able to return multiple lists at once.

2
  • return immediately exists the execution of the function. All Python functions can return exactly one object. Note, list1, list2 is actually a tuple-literal, it is the commas that make the tuple, not the parentheses! So, usually, the strategy is to accumulate all the values you want to return in some other data-structure (e.g. a list) and return that. Alternatively, you could use a generator which usees yield instead of return, and this will have semantics close to what you want, however, it creates a generator (a type of iterator) and is a bit more advanced Commented Dec 12, 2017 at 18:59
  • You might want to look into yield: stackoverflow.com/a/231855/369 Commented Dec 12, 2017 at 19:01

2 Answers 2

2

Here's my suggestion. Creating a 'major_array' and adding 'part_result' in that array on each iteration of loop. This way if your loop iterates 10 times, you will then have 10 arrays added in your 'major_array'. And finally the array is returned when the for loop finishes. :)

def readTXT():
    #create a new array
    major_array = [] 
    part_result = []
    '''Reading all data from text file'''
    with open('dataset/sometext.txt', 'r') as txt:
        for lines in txt:
            part = lines.split()
            part_result = [int(i) for i in part]
            #sorted([(p[0], p[14]) for p in part_result], key=lambda x: x[1])
            print(part_result)
            major_array.append(part_result)
         return major_array
Sign up to request clarification or add additional context in comments.

9 Comments

Can you provide an explanation?
Don't beg for upvotes. If it's a good answer, it'll be upvoted without needing to ask for them. Also, I'd suggest not saying that an answer is simple. To someone learning the basics, it doesn't feel simple and saying that it is comes across as condescending.
Sure thing! I suggested creating a 'major_array' and adding 'part_result' in that array on each iteration of loop. This way if your loop iterates 10 times, you will then have 10 arrays added in your 'major_array'. And finally the array is returned when the for loop finishes. :)
=D Keep up the good work. This site is nothing if there aren't people doing their best to answer questions.
This was exactly, what I was looking for. And thanks for adding explanation.
|
0

Here is a solution:

def readTXT():
    with open('dataset/sometext.txt') as lines:
        all_lists = []
        for line in lines:
            all_lists.append([int(cell) for cell in line.split()])
        return all_lists

Note that the return statement is outside of the loop. You get only one list because you return inside the loop.

For a more advanced user, this solution is a shorter and more efficient but at the cost of being a little hard to understand:

def readTXT():
    with open('dataset/sometext.txt') as lines:
        return [[int(x) for x in line.split()] for line in lines]

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.