I have a file containing large strings, and I need to scramble each and every one of them. This is a portion of my code which does the scrambling.
for line in filein:
zzz = line.strip('\n').split()
yyy = list(zzz)
random.shuffle(yyy)
zzzz = ''.join(yyy)
fout.write(zzzz + '\n')
I found something like this online and so tried to use it here. Except that the original file and the file supposed to containing the scrambled lines... are identical.
But if I say copy one of the lines, and do this in the python interpreter directly...
>>>import random
>>>zzz = "..." ###(one of my large strings would go here, for sake of space didn't put it in)
>>>yyy = list(zzz)
>>>random.shuffle(yyy)
>>>zzzz = ''.join(yyy)
>>>zzzz
the string that gets printed out is a scrambled version of my original string. Am I forgetting to do something really simple and stupid? Or did I miss something important?
line.split
returns a list that is only 1 element in length (the line itself) since your data presumably has no whitespace interspersed with it. Then you turn it into a list (which does nothing other than make a shallow copy) so you end up shuffling a list of length 1 -- which leads to a not-so-random result :-).
The fix is to get rid of the split
:
for line in filein:
zzz = line.rstrip('\n')
yyy = list(zzz)
random.shuffle(yyy)
zzzz = ''.join(yyy)
fout.write(zzzz + '\n')