R
R
rubbermanMichelinman2019-10-28 16:04:54
Python
rubbermanMichelinman, 2019-10-28 16:04:54

How to set N number of arrays?

The first line of the input file contains two natural numbers w and h. The second line contains an integer n - the number of rectangles. The next n lines contain information about all the rectangles. Each line describes one rectangle as four numbers x1,y1,x2,y2, where (x1,y1) and (x2,y2) are the coordinates of the upper left and lower right corners of the rectangle, respectively.
I highlighted the question in bold. How to create it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2019-10-28
@rubbermanMichelinman

Read into a nested (2-D) list. If the format is there as in the description, separated by commas, then you can fill it out like this:

with open("myfile.txt") as f:
    # считываем w, h, n из первых 2-х строк через f.readline()
    # затем до конца считываем все прямоугольники в массив
    rects = [list(map(int, r.split(','))) for r in f.splitlines()]

As a result, rects will be filled with lists of 4 coordinates for each rectangle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question