I've been using slice operations a lot since I started studying python, so I'd like to summarize it here for my own confirmation.
First of all, from the array, in python, the created array is automatically numbered (called index), and the number of the contents created starting from 0 is assigned.
l = [5,3,1,2]
print(l.index(5))
print(l.index(3))
print(l.index(1))
print(l.index(2))
$ python atcoder.py
0
1
2
3
It turns out that 0123 is assigned in order to 5,3,1,2 in the array like this. By the way, .index () of l.index () is a function that can be used for things with index such as arrays, and you can get the index number of the specified place.
Next, we will use that index to retrieve the values in the array.
l = [5,3,2,1]
print(l[0])
print(l[2])
$ python atcoder.py
5
2
Add [] to the array like this and specify the index you want to retrieve in it. If you set l [0], you can get the 0th 5th.
Next, take the contents of multiple arrays instead of one.
l = [5,3,2,1]
print(l[0:2])
$ python atcoder.py
[5, 3]
When you want to take multiple values, as [Index: Index], you can see it. You can get from the number specified at the beginning to the range of the number specified later. Please note that if you set [0: 2] together with a for statement etc., you will get the value before 2 that is, in this case, from 0 to 1.
Next is the method from the beginning to the end, or from the beginning to the end
l = [5,3,2,1]
print(l[1:])
$ python atcoder.py
[3, 2, 1]
l = [5,3,2,1]
print(l[:3])
$ python atcoder.py
[5, 3, 2]
The first one retrieves the value from the index specified by [Index:] to the end. The latter is up to the index specified from the beginning with [: Index], and the latter is up to one before as explained earlier.
This may not happen in everyday life, but the numbers are lined up side by side, and where do you divide them to reduce the difference in size between the left and right? Slicing operation is convenient in such cases.
l = [3,14,6,40,2,6,17,4,6,7,38,5,44,22,1,6,6,7]
for i in range(1,len(l)):
l_l = l[:i]
print(f"l_l{l_l}")
l_r = l[i:]
print(f"l_r{l_r}")
By increasing the value of a certain number up to a certain number and the value of a certain number from a certain number one by one, it can be divided into right and left at all positions. If the range is from 1 to 0, l_l in the first loop becomes l [: 0], and an empty array is completed. There is no need to compare the sky with the others, so I did so.
l_l[3]
l_r[14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14]
l_r[6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6]
l_r[40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40]
l_r[2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2]
l_r[6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6]
l_r[17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17]
l_r[4, 6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4]
l_r[6, 7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6]
l_r[7, 38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7]
l_r[38, 5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38]
l_r[5, 44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5]
l_r[44, 22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44]
l_r[22, 1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22]
l_r[1, 6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1]
l_r[6, 6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6]
l_r[6, 7]
l_l[3, 14, 6, 40, 2, 6, 17, 4, 6, 7, 38, 5, 44, 22, 1, 6, 6]
l_r[7]
So I should take all the differences from here, it is not related to this article, but I will write it for the time being
l = [3,14,6,40,2,6,17,4,6,7,38,5,44,22,1,6,6,7]
min_g = 10000000
ans = 0
for i in range(len(l)):
l_l = l[:i]
l_r = l[i:]
if min_g > abs(sum(l_l)-sum(l_r)):
min_g = abs(sum(l_l)-sum(l_r))
ans = i
print(ans)
$ python atcoder.py
10
It seems that there is no difference in the method of dividing the index numbers up to 10th and from 10th.
There are still a lot of slices and it is difficult to write them all, so I wrote the ones that I use a lot in the basics.
Recommended Posts