Start studying: Saturday, December 7th Books used: Miyuki Oshige "Details! Python3 Introductory Note ”(Sotec, 2017)
[Conditional branching, repetition, exception handling (Ch.5 / p.129)] Resume from (4th day), Finished until [Sort elements in the list (Ch.6 / p.170)](6th day)
As with the while statement, the else block is executed when the iteration ends. It is not executed when the for statement breaks in the middle.
By handling the expected error in advance, the process is performed to the end without breaking in the middle. Incorporate exception objects with try ~ except. You can define more than one except below. By defining with as, the exception object can be referenced by other variables.
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
words = ["flowers", "snow", "moon"]
mixture = [1, 2, "flowers", 3, 5] #Element types can be mixed.
num = [0] * 5 = [0, 0, 0, 0, 0]
It is possible to list convert values of other types by using list (). Type list (range (-5, 6)) to get a list of values in the specified range. (In this case, -5 to 6) If you type list ("ABCDE"), it will be divided character by character like "A", "B", "C", ...
words = ["flowers", "snow", "moon"] If you type words [1], the first "snow" in the list will be output. Type words [1] = "wind" to replace "snow" with "wind". You can check the number of elements that are the length of the list with len (). If it is len (words), it is 3, but since the index number starts from 0, for example, word [3] gives an error.
Append with append (value). Since it is a list object method, it is necessary to prepare a list in advance. Insert with insert (position, value). The insertion location conforms to the index number. Pull out with pop (pull out position). Since pop returns the deleted value at the same time as deleting it, it is an image to be taken out of the box. remove removes. If there are multiple values you want to delete, delete only the first value found. If you want to erase everything, combine it with a while statement.
Split by string.split (separator). A separator is a division standard in a sentence (understood). If usa = "yes we can", type words = usa.split () based on the space between characters. Divided into each word. If usa = "yes, we, can", usa.split (,) will be the separator. If spaces or commas are mixed, you can replace it with replace. (Separator) .join (list) allows you to join each element of the list with a separator. If you type AMERICA = OBAMA.join (usa), you will get AMERICA = "YES ** OBAMA ** WE ** OBAMA ** CAN" ".
Lists can be concatenated with the + operator. It can also be combined with extend (). Whereas append is the append of an element, extend is the list itself. EEIAA = ["kimi", "kara", "morainaki"] With EEIAA [1:] ["kara", "morainaki"] With EEIAA [: 2] ["kimi", "kara", "morainaki"] I feel that I had a lot of chances to see this in the pre-processing of machine learning data. By defining a name for each, you can ** list split **.
Recommended Posts