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.

Viktoria
  • 533
  • 2
  • 7
  • 24
  • `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 – juanpa.arrivillaga Dec 12 '17 at 18:59
  • You might want to look into `yield`: https://stackoverflow.com/a/231855/369 – Blorgbeard Dec 12 '17 at 19:01

2 Answers2

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
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]
Hai Vu
  • 37,849
  • 11
  • 66
  • 93