Is there a way to randomly shuffle what keys correspond to what values? I have found random.sample but I was wondering if there was a more pythonic/faster way of doing this.
Example: a = {"one":1,"two":2,"three":3}
Shuffled: a_shuffled = {"one":2,"two":3,"three":1}
sorry the only way to make it faster is by using numpy :/. No matter what you do it has to somehow scramble all the indices which takes time - so doing it in C will help slightly. Also the difference between sheer random and this random is that you can't have repeated indices.
sorry it's sort of long now - so you'll have to do some scrolling
Using Choice/Permutation may look nicer - but it's not faster by any means. Unfortunately copying is usually slow unless it's a small size - and there's no way to pass pointers/references without it having to take up an extra line - though I debate if this makes it 'non-pythonic'
namely if you look at the Zen of Python or just do
import this
in a python session one of the lines is:so it's open to interpretation of course :)
Or, more pithy would be:
(but I suppose that is the solution you already came up with.)
Note that although using
random.sample
is pithier, usingrandom.shuffle
is a bit faster: