jeudi 11 janvier 2018

Python shuffle sublists of a list

I have a list of lists in python: l = [ [1,2,3], [4,5,6], [7,8,9] ] and I want to shuffle each sublist. How can I achieve this?

I have tried the following:

import random

x = [ [1,2,3], [4,5,6], [7,8,9] ]

random.shuffle(x) # This shuffles the order of the sublists,
                  # not the sublists themselves.

x = [ random.shuffle(sublist) for sublist in x ] # This returns None
                                                 # for each sublist.

print(x)    




Aucun commentaire:

Enregistrer un commentaire