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.
returnimmediately exists the execution of the function. All Python functions can return exactly one object. Note,list1, list2is 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 useesyieldinstead 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 advancedyield: stackoverflow.com/a/231855/369