Just started studying python and faced with a task for which I can not find a solution. There is a .txt file in which 2 lines of integer data

The first line is the total amount of subsequent data.

Second - The data itself is separated by a space.

Those. something like this:

four

4 82 30 29

The task is to fill the array with numbers from the second line.

Can you give at least a tip on what you need to google and study?

    1 answer 1

    I, of course, also do not have an ass in python, but I don’t really understand why the first line is needed here at all ... if the input had a format like:

    4 4 82 30 29 

    Then there would be at least some meaning. And so:

     file = open('input.txt').readlines() arr = [int(i) for i in file[1].split()] 

    Or so:

     with open('input.txt', 'r') as file: arr = [int(i) for i in file.read().splitlines()[1].split()] file.close() 

    And you need to google the basics. I can recommend pythontutor.ru as a starting point

    • Probably to be misleading. I didn’t have the task for myself - Leniork
    • I would add the condition len (file)> 0 in front of the generator or in the generator itself, otherwise an exception is possible if the file does not have the required number of lines. - garrythehotdog
    • For good, if you score on the type of data that is added to the list, then there is no need for a generator there at all, but at the expense of the condition, then if it was about the application the user is working with, then yes ... , but here, apparently, we are talking about a training task, in which, as a rule, all introductory (at least) formally correspond to the condition, therefore I think that this can be neglected - Andrey