LoginSignup
0
1

More than 5 years have passed since last update.

Numpy > Link: 複数のリストの要素を同じ順番で入れ替える

Last updated at Posted at 2017-09-02
動作環境
ideone(Python3)

2つのnumpy.array(以下、array)があるとする。
1つのarrayを入替え、もう1つのarrayも同じ順番で入替えたい。
(ニホンゴムズカシイ)

https://stackoverflow.com/questions/4601373/better-way-to-shuffle-two-numpy-arrays-in-unison

answered Jan 5 '11 at 8:52
mtrw
のコードが参考になりました。

ideoneでの実装例。
https://ideone.com/pw7L27

import numpy as np


def unison_shuffled_copies(a, b):
    assert len(a) == len(b)
    p = np.random.permutation(len(a))
    return a[p], b[p]

alist = np.array([ 1, 2, 3, 4, 5 ])
blist = np.array([ 3, 1, 4, 1, 5 ])
ares, bres = unison_shuffled_copies(alist, blist)
print(ares)
print(bres)

run
[2 1 5 3 4]
[1 3 5 4 1]
0
1
0

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
0
1