Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python, shuffle a array of list in a specifiic range?

i want to ask if i got a array of list in python like this:

a = [[1, 2, 3, 4, 5, 6], 
     [1, 2, 3, 4, 5, 6],
     [1, 2, 3, 4, 5, 6]]

How do i shuffle within range a[1][1] to a[1][4]?

I only know the normal shuffle

random.shuffle(a)
like image 919
FastBatteryCharger Avatar asked Dec 28 '25 04:12

FastBatteryCharger


1 Answers

Take a sample from the sublist, with the sample size set to the same length, then assign that sample back to the slice:

a[1][1:4] = random.sample(a[1][1:4], 3)

This takes a sample of the 3 elements from the source list (producing those same 3 elements in random order), and assigns these right back to the same indices.

like image 77
Martijn Pieters Avatar answered Dec 30 '25 18:12

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!