I'm trying to randomly shuffle one element within a nested list of lists.
E.g. I have:
list1=[[['a', 'b', 'c', 1], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 4]], [['a', 'b', 'c', 5], ['a', 'b', 'c', 6]]]
I want to randomly shuffle the 3rd element of each sub-list (the float) while keeping the rest of the list entries and their structure within this nested list intact. E.g. as such:
list1=[[['a', 'b', 'c', 1], ['a', 'b', 'c', 6]], [['a', 'b', 'c', 4], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 5]]]
So far, I've come up with the following:
import random
list1 = [[['a', 'b', 'c', 1], ['a', 'b', 'c', 2]], [['a', 'b', 'c', 3], ['a', 'b', 'c', 4]], [['a', 'b', 'c', 5], ['a', 'b', 'c', 6]]]
vals = [x[3] for line in list1 for x in line]
shuffled_vals = random.sample(vals,len(vals))
counter = 0
for i in range(len(list1)):
for j in range(len(list1[i])):
list1[i][j][3] = shuffled_vals[counter]
counter += 1
Although this works as intendend, I'd be curious if there is a more elegant / Pythonic solution. Also, I'm not sure how well this scales - the list I intend to use it on will contain several million entries.
Any tips for improving this code (to a more Pythonic or more efficient one) are greatly appreciated.
Your code looks good to me. I would do almost the same except I wouldn't mutate (modify in-place) the input list. Usually you would also avoid using range(len(X)) and prefer iterating over the elements of X directly.
Putting this together that would give something like this:
from random import sample
vals = [x[3] for line in list1 for x in line]
vals = sample(vals, len(vals))
new_list = [[sub_l[:3] + [vals.pop()] for sub_l in l] for l in list1]
Here I also make use of .pop() because we can throw the vals away as soon as we've used them. Which save me from using something like the counter you had.
Maybe a small improvement would be to use mutation where we have perfect control over the object. Using random.sample() is quite handy if you want to avoid mutation, but if you don't care you can simply replace vals = sample(vals, len(vals)) by random.shuffle(vals). Which I feel is a bit better here since it's more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With