Python randomly shuffles the elements in the list

Randomly shuffle the elements in the list#

Write your own function to randomly shuffle the elements in the list

Option 1: Exchange method##

Randomly select the original list index, exchange the value at the index position

import random

def random_list1(li):for i inrange(0,100):
  index1 = random.randint(0,len(li)-1)
  index2 = random.randint(0,len(li)-1)
  li[index1], li[index2]= li[index2], li[index1]return li

li =[1,2,3,4,5]
test =random_list1(li)print(test)

Option 2: Randomly select and re-add to a list##

First generate a copy of the original list a_copy, create a new empty list result, and then randomly select the values in the copy list to store in the empty list result, and then delete

import random

def random_list2(a):
 a_copy = a.copy()
 result =[]
 count =len(a)for i inrange(0, count):
  index = random.randint(0,len(a_copy)-1)
  result.append(a_copy[index])
  del a_copy[index]return result

test =[1,3,4,5,6]
result =random_list2(test)print(result)

Solution 3: The system comes with a function shuffle

import random

test =[1,2,3,4,5]
random.shuffle(test)print(test)

Python's random.shuffle() function can be used to shuffle a sequence. It shuffles the sequence itself, rather than generating a new sequence.

Attachment: shuffle function in python##

def shuffle(self, x, random=None):"""Shuffle list x in place, and return None.

 Optional argument random is a 0-argument function returning a
 random float in[0.0,1.0);if it is the default None, the
 standard random.random will be used."""

 if random is None:
  randbelow = self._randbelow
  for i inreversed(range(1,len(x))):
   # pick an element in x[:i+1]with which to exchange x[i]
   j =randbelow(i +1)
   x[i], x[j]= x[j], x[i]else:
  _ int = int
  for i inreversed(range(1,len(x))):
   # pick an element in x[:i+1]with which to exchange x[i]
   j =_int(random()*(i +1))
   x[i], x[j]= x[j], x[i]

Recommended Posts

Python randomly shuffles the elements in the list
Explain the list under Python multithreading in detail
The usage of wheel in python
​What are the numbers in Python?
Talking about the modules in Python
The usage of tuples in python
The usage of Ajax in Python3 crawler
What is the function of adb in python
How to use the round function in python
How to use the zip function in Python
An article to understand the yield in Python
What does the tab key in python mean
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
How to use the format function in python
First acquainted with python, the grammatical rules in python
python list learning
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very
How to install the downloaded module in python
The best way to judge the type in Python
[902] python list sort
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Python data visualization: who are the big names in Python?
For the first time in history, Python surpassed Java!
What are the ways to open files in python
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
Concurrent requests in Python
Consolidate the Python foundation (2)
Context management in Python
Arithmetic operators in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
How to find the area of a circle in python
Learn the hard core operation of Python in one minute