How to shuffle a part of a Python list (at random.shuffle)

Introduction

I couldn't find a Japanese article, so I wrote it.

Outline of random.shuffle

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]

How to shuffle some of the contents of the list ...?

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.

Remarks

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.

Recommended Posts

How to shuffle a part of a Python list (at random.shuffle)
How to write a list / dictionary type of Python3
[Python] How to make a list of character strings character by character
How to get a list of built-in exceptions in python
[Python] How to convert a 2D list to a 1D list
Summary of how to use Python list
[Python] How to put any number of standard inputs in a list
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to format a list of dictionaries (or instances) well in Python
[Python] How to use list 1
How to pass the execution result of a shell command in a list in Python
How to get a list of files in the same directory with python
How to write a Python class
How to run a Python file at a Windows 10 command prompt
How to identify the element with the smallest number of characters in a Python list?
How to develop in a virtual environment of Python [Memo]
How to display a list of installable versions with pyenv
How to get the last (last) value in a list in Python
[Python] How to use list 3 Added
How to check in Python if one of the elements of a list is in another list
How to get a list of links from a page from wikipedia
How to connect the contents of a list into a string
[Python] How to delete rows and columns in a table (list of drop method options)
[Python] How to create a table from list (basic operation of table creation / change of matrix name)
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to determine the existence of a selenium element in Python
[Python] How to force a method of a subclass to do something specific
Try to get a list of breaking news threads in Python.
[Python] How to get divisors of natural numbers at high speed
How to check the memory size of a variable in Python
I tried to create a list of prime numbers with python
How to check the memory size of a dictionary in Python
How to save only a part of a long video using OpenCV
How to remove duplicates from a Python list while preserving order.
How to delete multiple specified positions (indexes) in a Python list
[Python] How to make a matrix of repeating patterns (repmat / tile)
[Python] A program that rotates the contents of the list to the left
[Python] Summary of how to use pandas
[Python] How to make a class iterable
Display a list of alphabets in Python 3
[Python] How to invert a character string
How to get a stacktrace in python
[Python2.7] Summary of how to use unittest
[python] Get a list of instance variables
[Python2.7] Summary of how to use subprocess
[Python] Get a list of folders only
How to run a Maya Python script
[Question] How to use plot_surface of python
How to crop the lower right part of the image with Python OpenCV
How to send a visualization image of data created in Python to Typetalk
How to put a line number at the beginning of a CSV file
[Python] How to read a csv file (read_csv method of pandas module)
[Python] How to use list 2 Reference of list value, number of elements, maximum value, minimum value
I want to color a part of an Excel string in Python
How to calculate the volatility of a brand
[Python] List Comprehension Various ways to create a list
[python] How to display list elements side by side
How to read a CSV file with Python 2/3
A simple example of how to use ArgumentParser
[Python] How to use two types of type ()
How to create a Python virtual environment (venv)