0

I have a list of numbers and I want to shuffle it with a key and redo it. I am using it as a small encryption algorithm so I need to re-shuffle or get the original list from the suffled list.

original = [10, 20, 30, 25, 45, 68, 25]
shuffled = shuffle(original, key=10)
print shuffled
# >>> [25, 30, 25, 10, 20, 45, 68]
print re_shuffle(shuffled, key=10)
# >>> [10, 20, 30, 25, 45, 68, 25]

This is the idea of what I want. Is there a library or algorithms for this ?

Muhammed K K
  • 1,100
  • 2
  • 8
  • 19
  • 1
    How is `key` relevant to your shuffle algorithm? – Abhijit Jan 24 '14 at 16:23
  • If there is no key then how can re_shuffle the array ? There has to be some parameter for the algorithm so that it can be retraced to the original list. – Muhammed K K Jan 24 '14 at 16:25
  • 1
    You can seed your shuffeling in python, see: http://stackoverflow.com/questions/4557444/python-random-sequence-with-seed - just be advised that self invented encryption algorithms are almost always bad. Good for trying things and toying around, but you should never rely on those, unless you are an encryption expert – kratenko Jan 24 '14 at 16:25
  • @kratenko Its for a school students project, so the algorithm is fine. – Muhammed K K Jan 24 '14 at 16:26
  • duplicate of question linked by @kratenko – njzk2 Jan 24 '14 at 16:27
  • "reshuffle" makes it sound like you want to be able to go from `original` to `shuffled` in a consistent way. Do you need that? Or do you just need to be able to unshuffle to get back to `original`? – Teepeemm Jan 24 '14 at 16:29
  • @kratenko Thanks. It seems the link you posted is what I wanted. – Muhammed K K Jan 24 '14 at 16:29
  • @Teepeemm unshuffle is what I need. – Muhammed K K Jan 24 '14 at 16:30
  • @MuhammedKK Ideally you would just make two objects. `original == [10, 20, 30, 25, 45, 68, 25]` and `shuffled == [25, 30, 25, 10, 20, 45, 68]` As kratenko pointed out, you could write your own algorithm to shuffle so it's undoable, but it's generally a Bad Idea. – Adam Smith Jan 24 '14 at 16:37

1 Answers1

0
from random import shuffle

x = [[i] for i in range(10)]
shuffle(x)

print x
Nabin
  • 11,216
  • 8
  • 63
  • 98