1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NumPy > [1, 2, 3, 4, 5, 6, 7, 8, 9]を3つのndarray([1,4,7], [2,5,8], [3,6,9])に分割する実装 > { スライスを使う方法 / reshape()+行列転置の方法 } を教えていただきました

Last updated at Posted at 2017-04-09
動作環境
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)
実行
$ 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.]

目的は果たしたが、もっと簡潔な方法はあるだろう。

教えていただいた事項

@shiracamus さんのコメントにてスライスを使ったシンプルな方法を教えていただきました。

情報感謝です。

@tuki0918 さんのコメントにてreshape()と行列の転置を使った方法を教えていただきました。

情報感謝です。

1
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?