I couldn't find a Japanese article, so I wrote it.
There is a "random" module in the Python standard library. Among them is shuffle, a very useful function that shuffles the list.
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 2 2016, 17:53:06) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>>import random >>> list_a = [1,2,3,4,5] >>> list_a [1, 2, 3, 4, 5] >>> random.shuffle(list_a) >>> list_a [1, 3, 4, 5, 2]
When you want to shuffle some of the contents of the list, the first thing you might think of is It might be this way.
>>> random.shuffle(list_a[0:3]) >>> list_a [1, 2, 3, 4, 5]
?? Doesn't it work?
You can shuffle only part of it by taking the following method. >>> list_b=list_a[0:3] >>> list_b [1, 2, 3] >>> random.shuffle(list_b) >>> list_b [3, 2, 1] >>> list_a[0:3]=list_b >>> list_a [3, 2, 1, 4, 5]
I was able to shuffle only a part of it properly.
Why did "random.shuffle (list_a [0: 3])" not work ... I have my own explanation. I may write it as a separate article when I have time at a later date. Also, if you have a better implementation example or convenient means, please let us know.