[PYTHON] NumPy> [1, 2, 3, 4, 5, 6, 7, 8, 9] with 3 ndarrays ([1,4,7], [2,5,8], [3,6,9]) Implementation to divide into> {How to use slice / reshape () + Method of matrix transpose}

Operating environment


GeForce GTX 1070 (8GB)
ASRock Z170M Pro4S [Intel Z170chipset]
Ubuntu 14.04 LTS desktop amd64
TensorFlow v0.11
cuDNN v5.1 for Linux
CUDA v8.0
Python 2.7.6
IPython 5.1.0 -- An enhanced Interactive Python.
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)

v0.1

test_python_170409a.py


import numpy as np

alist = [1, 2, 3, 4, 5, 6, 7, 8, 9]

xs = np.array([])
ys = np.array([])
zs = np.array([])

for idx, elem in enumerate(alist):
    print('%d:%s' % (idx, elem))
    if idx % 3 == 0:
        xs = np.append(xs, elem)
    elif idx % 3 == 1:
        ys = np.append(ys, elem)
    else:
        zs = np.append(zs, elem)

print(xs)
print(ys)
print(zs)

Run


$ python test_python_170409a.py 
0:1
1:2
2:3
3:4
4:5
5:6
6:7
7:8
8:9
[ 1.  4.  7.]
[ 2.  5.  8.]
[ 3.  6.  9.]

It served its purpose, but there may be a simpler way.

Matters taught

@ shiracamus's Comment taught me a simple method using slices.

Thank you for the information.

@ tuki0918's Comment taught me how to use reshape () and matrix transpose.

Thank you for the information.

Recommended Posts

NumPy> [1, 2, 3, 4, 5, 6, 7, 8, 9] with 3 ndarrays ([1,4,7], [2,5,8], [3,6,9]) Implementation to divide into> {How to use slice / reshape () + Method of matrix transpose}
Explain how to use TensorFlow 2.X with implementation of VGG16 / ResNet50
How to use numpy
[Python] What is a slice? An easy-to-understand explanation of how to use it with a concrete example.
Summary of how to use pandas.DataFrame.loc
How to use virtualenv with PowerShell
Summary of how to use pyenv-virtualenv
How to use FTP with Python
Summary of how to use csvkit
[Python] Summary of how to use pandas
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to calculate Use% of df command
How to use Ass / Alembic with HtoA
How to use Japanese with NLTK plot
[Python2.7] Summary of how to use unittest
How to use jupyter notebook with ABCI
Jupyter Notebook Basics of how to use
How to use CUT command (with sample)
Basics of PyTorch (1) -How to use Tensor-
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to use SQLAlchemy / Connect with aiomysql
Comparison of matrix transpose speeds with Python
[Question] How to use plot_surface of python
How to use JDBC driver with Redash
Find out how to divide a file with a certain number of lines evenly
[Numpy, scipy] How to calculate the square root of a semi-fixed definite matrix
How to use GCP trace with open Telemetry
How to use folium (visualization of location information)
A simple example of how to use ArgumentParser
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Not much mention of how to use Pickle
Summary of how to use MNIST in Python
How to specify attributes with Mock of python
How to implement "named_scope" of RubyOnRails with Django
Numpy leave? !! Partial differential of matrix with Sympy
Story of trying to use tensorboard with pytorch
How to use tkinter with python in pyenv
How to return multiple indexes with index method
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)