[python] Array slice operation

Checking array slicing operations often used in python

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.

Array manipulation

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.

application

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.

Summary

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

[python] Array slice operation
Python slice
python string slice
[python] vector operation
Array operation etc. 1
Python multidimensional array
[Beginner] Python array
Python OS operation
Python slice basics
[Python] Matrix operation
Python array basics
[Python] Operation of enumerate
Correspondence summary of array operation of ruby and python
python numpy array calculation
Python directory operation summary
Python logical operation stumbling
Python decorator operation memo
Python memo (for myself): Array
Quicksort an array in Python 3
Python: 3D array image (numpy.array)
S3 operation with python boto3
Python and ruby slice memo
Stumble story with Python array
Create a python numpy array
Slice error in python (´ ; ω ; `)
Python 2D array trap [Copy array]
How to slice a block multiple array from a multiple array in Python
Python pywin32 (win32com) Excel operation memorandum
[Python] Operation memo of pandas DataFrame
Subscript access to python numpy array
Python
slice
[python] week1-3: Number type and operation
Slice without using Python, colon (:). a.__getitem__ (slice (3,5)).
Python application: Numpy Part 3: Double array
[Python] How to swap array values
Basic Python operation 2nd: Function (argument)
Python> What is an extended slice?
[Automation with python! ] Part 2: File operation
[Python] File operation using if statement
Mouse operation using Windows API in Python
Python basic operation 1st: List comprehension notation
Python + Selenium Frequently used operation method summary
Two rules when reading Python (slice notation)
Python> link> 2D array initialization and assignment
Python / numpy> list (numpy array) file save / load
[Python] Summary of array generation (initialization) time! !! !!
[Python beginner memo] Python character string, path operation
[Introduction to Udemy Python3 + Application] 17. List operation
Python basic operation 3rd: Object-oriented and class
Python data structure and operation (Python learning memo ③)
What I did with a Python array
[Python] Combine all the elements in the array