Reputation: 384
I have a list which has nested lists. These nested lists are sentences. I want to randomize the order of sentences, but the random.shuffle()
method does not supported nested lists.
[['A', 'B'],['C','D'],['E','F']]
I only want to reorder the sentences (randomly) not the words in the sentence, how can I achieve that?
Upvotes: 0
Views: 329
Reputation: 12031
This might help you as example
import random
def deep_shuffle_iter(i):
try:
map(shuffle_iter, random.shuffle(i))
except TypeError: pass
It does shuffle of iters using recursion
e.g.
>>> asd = [[0,1],[2,3],[4]]
>>> shuffle_iter(asd)
>>> asd
[[4], [2, 3], [0, 1]]
Upvotes: 0
Reputation: 5916
shuffle
is working fine for me.
>>>from random import shuffle
>>>l=[['a','b'],['c','d'],['e','f']]
>>>shuffle(l)
>>>l
[['c', 'd'], ['a', 'b'], ['e', 'f']]
>>>shuffle(l)
>>>l
[['c', 'd'], ['e', 'f'], ['a', 'b']]
Upvotes: 3