A
A
argumentum2016-08-01 08:19:42
Python
argumentum, 2016-08-01 08:19:42

What is the best way to model a table as a two-dimensional list?

Good day.
There are two ways to model a table using a two-dimensional list:
1) The first index will be, as it were, columns, and the second index, respectively, rows
2) On the contrary, the first index will be rows, the second - columns.
The first way, in my opinion, is more convenient, since it is easy to manipulate the columns. For example, if the table has 5 columns, but you need to leave the first and fourth - list[0] will be the first column with all rows, list[3] - the fourth.
In addition, if you need to process data in separate columns (which is often necessary) - it's easier to process this column, pass it as a parameter to the function.
But, interestingly, in fairly solid sources, the second approach is used - when the rows are represented by the first index, and the columns - by the second. (What specific sources were already forgotten, since when I met this it was irrelevant, one of them seems to be the pandas library)
I would like to know how it would be more correct and why.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nicholas, 2016-08-01
@healqq

It is better not to use inappropriate data structures. In this case, the list is a completely inappropriate data structure. Use a regular two dimensional array. As a rule, the first index operates on columns, the second on columns.
Your cases are solved in an elementary way with the help of various functions for working with arrays (they are in many languages). For example, in order to leave 1 and 5, you can use map.
Obviously, the most common case is to filter rows by some filter. Your data organization is no good for that. And in the case of storing strings - everything is elementary.

A
Alexander Movchan, 2016-08-01
@Alexander1705

There really is no difference, rows and columns are just a matter of your interpretation.
You can think of the first index as rows, then you can operate on a single row, you can think of the first index as a column if you need to operate on separate columns. You simply choose the method that suits your task.
If there is no difference, indeed, it is generally accepted that the first index is the row number, this is more convenient for displaying and entering tables.

A
Alexander, 2016-08-01
@fireSparrow

As other commentators have already said, more often you still need to perform operations on strings (adding, deleting, selecting by filter).
If you expect that you won't need this, but you will need column operations, you can use whichever approach you think is best for your particular case.
But, IMHO, the best thing would be to simply write your own class, which will provide convenient interfaces for working with both rows and columns.
For example, in this class, table.rows[0] can display the first row and table.cols[0] the first column. Plus, additionally fasten the selection by rows and columns.
For example, like this table.select_cols(1,5)
Here everything depends on your imagination, you can implement as many convenient interfaces as you can imagine. And for you (or another user of this class) there will be no difference at all how the internal data structure is organized there, because you will only work with convenient interfaces that you write specifically for your tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question