lundi 19 mars 2018

Generate k random list from a list of elements possibly containing sublist

I have a list l in the following form. I need to randomly generate k (six in this example) number of lists from this list so that only one element is selected from the sublists at a time.

l = [1,2,3,[11,22,33,44], 4,5,6, [22,33,44], 5, [99,88]]

Result: 

1,2,3, 22, 4,5,6, 22 ,5, 88
1,2,3, 33, 4,5,6, 44 ,5, 88
1,2,3, 44, 4,5,6, 22 ,5, 99
1,2,3, 22, 4,5,6, 33 ,5, 99
1,2,3, 33, 4,5,6, 33 ,5, 99
1,2,3, 33, 4,5,6, 44 ,5, 88

I can write a for loop and pick a random element whenever I encountered a list. But i am looking for more elegany pythonic way to do this.




Aucun commentaire:

Enregistrer un commentaire