I think it will explain how I read slices. If you are a beginner and want to use Sassa and Slice, please read it.
When I googled "How to use python slices", all the articles were written in detail, so I will write them roughly.
All you have to do is keep 3 points.
Imagine that the index points between characters, with the first character at the left end being 0.
+---+---+---+---+---+---+
| P | y | t | h | o | n |
+---+---+---+---+---+---+
0 1 2 3 4 5 6
-6 -5 -4 -3 -2 -1
You can read the code in your head like a comment.
>>> num_list = [1, 5, 2, 3, 4]
>>> num_list[1:4:1] #Specify the index introduced in 1. Select 1st "from" 4th "to" with 1 "interval"
[5, 2, 3]
>>> num_list = [1, 5, 2, 3, 4]
>>> num_list[-4:] #Select "from" 4th behind
[5, 2, 3]
Now you can roughly read the code.
After that, please read while holding down two examples. You should be able to cover how to write.
>>> num_list = [1, 5, 2, 3, 4]
>>> num_list[:4:1] #If "from" is omitted, select from the first element
[1, 5, 2, 3]
>>> num_list[1::1] #If "to" is omitted, the last element is selected.
[5, 2, 3, 4]
>>> num_list[1:4:] #Select by 1 interval by omitting "Interval"
[5, 2, 3]
>>> num_list[1:4] #":" Of "to:" (If you omit "interval", you can omit the second colon)
[5, 2, 3]
>>> num_list[-4:] #Select from the 4th behind
[5, 2, 3, 4]
I'm sorry if it's hard to understand.
Recommended Posts