Until now, I wrote a program by describing the process in a relatively flat manner. In other words, it feels like I was writing the process as I came up with. Far from being agile, I felt like I was writing the process with my thoughts leaking out in my head, thinking "Is this the code?" Perhaps it was more like writing a manuscript than writing a program.
I've been studying a little, and I think this is fine. Even if it is not a process that is used repeatedly, try to write it as a function. Instead of processing the data while thinking about it when putting it in a variable, first import the raw data into the program so that it is handled as it is (no, just assign it to the variable), and extract the necessary data from there. Try to use it.
In both cases, I wanted the structure to be easy to understand when rewriting or rereading the program later. So I decided to start from where I could. Then I got hooked ...
The cause of the swamp I was addicted to this time was the latter. This is a case where I realized that I still can't understand the arrangement properly. Specifically, the code below.
list_jikken-00.py
#Set variables
ringo_int = 300
ringo_float = 256.1
ringo_list = [ringo_int, ringo_float]
#Check the type
print(type(ringo_int))
print(type(ringo_float))
print(type(ringo_list))
#Show value
print(ringo_int)
print(ringo_float)
print(ringo_list)
#Extract elements from an array
print(ringo_list[0][0])
print(ringo_list[0][1])
#Doboji? ?? ??
This is not a really addictive program, but a simplified experimental program that tries to find out what's wrong with a simpler program. But when I do this, on line 17,
TypeError: 'int' object is not subscriptable
But what's wrong? I say. The error is the same for the original program and the above experimental code. ringo_list is <class'list'>
when you check the type, and [223850, 67154]
when you print it, right? Why do you call it an int!
If you search with the above error code, there are many explanations that say "It is an error that you will always encounter when you can write a little code", and I am disappointed that it is a life that is not surprising at all (well, I knew that). But after all, I'm dealing with list instead of int, so I don't know where to fix it.
Well, since the raw data handled first in the original program was suitable for creating a two-dimensional array, it seems that the array is two-dimensional for some reason. When I saw the explanation that subscript means subscript, I realized that "that is not two-dimensional, isn't it?"
list_jikken-01.py
...
#Extract elements from an array
print(ringo_list[0])
print(ringo_list[1])
#I see!
I finally came across what I should do. If you debug so much and don't come up with the structure of the array, you'll be disgusted by the stiffness of your head.
Lastly, if ringo_list is not a two-dimensional array, it was written to extract elements from the two-dimensional array, so when the elements are extracted in the ringo_list [0]
part, int The value of was returned, but since [0] and [1] were added, the integer cannot be subscripted, that is, TypeError:'int' object is not subscriptable
. , I understand this. Chii, I remember.
From now on, it was a day of working from home that I firmly vowed to be more conscious of the structure of the array.
Recommended Posts