Until the last time, I learned how to create a list and operate from it. The list I created so far was a one-dimensional list. This time, we will look at creating a list of two or more dimensions.
The list I've learned so far has been a one-dimensional list. You could put strings and numbers in this list, and you could put any data type.
In fact, you can put more lists in the list. This is called a ** multidimensional list **.
Let's look at it concretely. Enter the following code in the ** Python Console **.
>>>lsls = [[23, 24], [37, 38], [41, 42]]
>>>lsls
[[23, 24], [37, 38], [41, 42]]
Each element of the list is a list. This is represented in a table as follows.
number | 0 | 1 | 2 |
---|---|---|---|
element | [23, 24] | [37, 38] | [41, 42] |
You can check each element by specifying the element number in ** lls ** while referring to the above table.
>>>lsls[2]
[41, 42]
So how do you get 42 out of [41, 42]? 42 in [41, 42] is the first element in the second element, so specify as follows.
>>>lsls
[[23, 24], [37, 38], [41, 42]]
>>>lsls[2][1]
42
In addition, I would like you to consider it as an introduction, but you can also create a 3D list as shown below.
>>>lslsls = [[[1, 2],[5, 6]], [[10, 11],[15, 16]], [[26, 27],[28, 29]]]
>>>lslsls
[[[1, 2], [5, 6]], [[10, 11], [15, 16]], [[26, 27], [28, 29]]]
>>>lslsls[2]
[[26, 27], [28, 29]]
>>>lslsls[2][0]
[26, 27]
>>>lslsls[2][0][1]
27
The table is as follows. You can see how the two-dimensional list is stored in the elements of the list.
number | 0 | 1 | 2 |
---|---|---|---|
element | [[1, 2],[5, 6]] | [[10, 11],[15, 16]] | [[26, 27],[28, 29]] |
The last 3D list is complicated, so up to a 2D list is fine. The actual data is often represented in tabular form. Keep in mind that you can also store such data in a two-dimensional list for processing.
Recommended Posts