[Python 3.8 ~] Rewrite arrays etc. at the same time as definition [tips]

Introduction

-I want to make array initialization compact-

That wish can be realized. Yes, it's 3.8 ~.

How to do

(Variable name:=Definition)[slice] =Change

Note) Even if you do not slice, an error will be thrown if you do not add [:] etc.

For example

** ~ I want to define a matrix in which 1 to 9 are lined up diagonally plus 1 every 3 rows ~ **

Such a mysterious situation is also this street

import numpy as np
(x:=np.diag(range(10)))[::3] += 1
x

out


array([[ 1,  1,  1,  1,  1,  1,  1,  1,  1,  1],
       [ 0,  1,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  2,  0,  0,  0,  0,  0,  0,  0],
       [ 1,  1,  1,  4,  1,  1,  1,  1,  1,  1],
       [ 0,  0,  0,  0,  4,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  5,  0,  0,  0,  0],
       [ 1,  1,  1,  1,  1,  1,  7,  1,  1,  1],
       [ 0,  0,  0,  0,  0,  0,  0,  7,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  8,  0],
       [ 1,  1,  1,  1,  1,  1,  1,  1,  1, 10]])

For example

** ~ I want to draw a matrix with my left and right inverted on a square matrix with 1 to 9 lined up diagonally ~ **

Even in such a case, this street

import numpy as np
(x:=np.diag(range(10)))[:] -= x[:, ::-1]
x

out


array([[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  1,  0,  0,  0,  0,  0,  0, -1,  0],
       [ 0,  0,  2,  0,  0,  0,  0, -2,  0,  0],
       [ 0,  0,  0,  3,  0,  0, -3,  0,  0,  0],
       [ 0,  0,  0,  0,  4, -4,  0,  0,  0,  0],
       [ 0,  0,  0,  0, -5,  5,  0,  0,  0,  0],
       [ 0,  0,  0, -6,  0,  0,  6,  0,  0,  0],
       [ 0,  0, -7,  0,  0,  0,  0,  7,  0,  0],
       [ 0, -8,  0,  0,  0,  0,  0,  0,  8,  0],
       [-9,  0,  0,  0,  0,  0,  0,  0,  0,  9]])

finally

The number of convenient walrus operator utilization series is increasing ...

Recommended Posts

[Python 3.8 ~] Rewrite arrays etc. at the same time as definition [tips]
Call the python debugger at any time
[Python] How to open two or more files at the same time
Loop variables at the same time in the template
At the time of python update on ubuntu
Turn multiple lists with a for statement at the same time in Python
Visualize data and understand correlation at the same time
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ②
I tried the same data analysis with kaggle notebook (python) and Power BI at the same time ①
Python built-in function ~ divmod ~ Let's get the quotient and remainder of division at the same time
wxPython: Draw animation and graph drawing at the same time
python memo: enumerate () -get index and element of list at the same time and turn for statement
Browse .loc and .iloc at the same time in pandas DataFrame
Count the number of times two values appear in a Python 3 iterator type element at the same time
How to read standard input or variable files at the same time like paste command in Python
See python for the first time
Get a datetime instance at any time of the day in Python
Set up a server that processes multiple connections at the same time
Plot multiple maps and data at the same time with Python's matplotlib