6
4

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.

Pythonでリセットできる(元に戻せる)shuffleをつくった

Last updated at Posted at 2016-12-07

Numpyのshuffleでリセットできるものがなかったようなので作ってみました。
そもそもあったり、もっといい書き方があったら教えてください。

resettable_shuffleでシャッフルして、reset_shuffleで元に戻します。

test.py
import numpy as np

# arrayは普通の1次元のリスト
# 破壊的メソッド
def resettable_shuffle(array, seed):
	np.random.seed(seed)
	np.random.shuffle(array)

# arrayは普通の1次元のリスト
# 非破壊的メソッド
def reset_shuffle(array, seed):
	seq = np.arange(len(array))
	np.random.seed(seed)
	np.random.shuffle(seq)
	tmp = np.c_[seq.T, np.array(array).T]
	tmp = np.ndarray.tolist(tmp)
	tmp = sorted(tmp)
	tmp = np.array(tmp)
	return np.ndarray.tolist(tmp[:,1])

seed = 321654
a = ["a","b","c","d","e","f"]

resettable_shuffle(a, seed)

print(a)

a = reset_shuffle(a, seed)

print(a)

出力結果

['a', 'b', 'c', 'd', 'e', 'f']
['c', 'f', 'd', 'e', 'a', 'b']
['a', 'b', 'c', 'd', 'e', 'f']

宣伝

フォローお願いします:golf: @redshoga

6
4
2

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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?