I made a shuffle that can be reset (reverted) with Python

It seems that there was nothing that can be reset with Numpy shuffle, so I made it. Please let me know if there is a better way to write it in the first place.

Shuffle with resettable_shuffle and restore with reset_shuffle.

test.py


import numpy as np

#array is an ordinary one-dimensional list
#Destructive method
def resettable_shuffle(array, seed):
	np.random.seed(seed)
	np.random.shuffle(array)

#array is an ordinary one-dimensional list
#Non-destructive method
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)

Output result

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

Promotion

Follow us: golf: @redshoga

Recommended Posts

I made a shuffle that can be reset (reverted) with Python
I made a familiar function that can be used in statistics with Python
I made a package that can compare morphological analyzers with Python
I made a module PyNanaco that can charge nanaco credit with python
I made a fortune with Python.
I made a daemon with Python
I made a plug-in that can "Daruma-san fell" with Minecraft
I made a character counter with Python
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
[Python] Make a graph that can be moved around with Plotly
[Python] I made my own library that can be imported dynamically
[python] I made a class that can write a file tree quickly
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
[Python] I made a utility that can access dict type like a path
I made a simple timer that can be started from the terminal
I made a tool that makes decompression a little easier with CLI (Python3)
I made a Christmas tree lighting game with Python
I made blackjack with python!
I registered PyQCheck, a library that can perform QuickCheck with Python, in PyPI.
I made a net news notification app with Python
I made a VM that runs OpenCV for Python
I made a Docker image that can call FBX SDK Python from Node.js
Python knowledge notes that can be used with AtCoder
I made a Python3 environment on Ubuntu with direnv.
A memo that I touched the Datastore with python
I made a python text
I made blackjack with Python.
A story that I was addicted to when I made SFTP communication with python
I made wordcloud with Python.
I made a LINE BOT that returns parrots with Go
I made a package to filter time series with python
I made a simple book application with python + Flask ~ Introduction ~
I made a puzzle game (like) with Tkinter in Python
I made a rigid Pomodoro timer that works with CUI
I investigated the pretreatment that can be done with PyCaret
Let's make a diagram that can be clicked with IPython
Can I be a data scientist?
I made a Line-bot using Python!
[Python] I made a function that decrypts AES encryption just by throwing it with pycrypto.
I made a program to collect images in tweets that I liked on twitter with Python
I made a module that can be glitched easily, but I can't pass arguments from entry_points
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
[Python] I made a Line bot that randomly asks English words.
I made a simple circuit with Python (AND, OR, NOR, etc.)
I made a library to easily read config files with Python
[Python3] I made a decorator that declares undefined functions and methods.
I want to use a wildcard that I want to shell with Python remove
[Python] A memo that I tried to get started with asyncio
I made a Nyanko tweet form with Python, Flask and Heroku
Make a Spinbox that can be displayed in Binary with Tkinter
I made a lot of files for RDP connection with Python