In this paper , We'll look at different implementations of selecting random elements from a list . In everyday projects , We often encounter this situation , For example, randomly select one or more data enhancement strategies to improve the diversity of training data .
Gossip , Let's start straight . :)
For the convenience of the example , Here we assume that there is a list containing multiple English names of fruits , And we need to choose a random fruit to output . The list of fruit names is as follows :
fruit_name_list = [
'apple',
'orange',
'banana',
'pear',
'watermelon'
]
random The library is a python Built in libraries for , So we don't need to reinstall it , You can easily import . Here, we will study how to use the library to select random elements from the list 3 Different ways .
function random.randrange(num_items)
Used to return an interval between 0 To num_items-1
A random number between , If we get a random subscript for a list , Then we can easily get a random element in the list .
The sample code is as follows :
import random
num_items = len(fruit_name_list)
random_index = random.randrange(num_items)
winner = fruit_name_list[random_index]
print(winner)
Output is as follows :
orange
function random.choice
Take the list as an input parameter , And return a random element from the list . In the above example , We can deliver list fruit_name_list
As an argument to this function .
The sample code is as follows :
winner = random.choice(fruit_name_list)
print(winner)
Output is as follows :
watermelon
function random.sample
Functions are similar to functions random.choice
, The main difference is that we can specify the number of random elements we need . In the following code example , We can get two random English names of fruits . random.sample
The return value is a list .
The sample code is as follows :
winners = random.sample(fruit_name_list,2)
print(winners)
Output is as follows :
['banana', 'watermelon']
Secrets
Library superior Random library , Because it's safer . And random Like the library , It is also a built-in python library , We don't have to install any other dependencies . however , If we use less than 3.6 Of Python edition , Must be used pip Install the module .
function secrets.randbelow
The function of is similar to random.randrange
. Using this function, we can get a value between 0 and num_items - 1
Random index between , Then use the index to easily access the elements in our English fruit name list .
The sample code is as follows :
import secrets
num_items = len(fruit_name_list)
random_index = secrets.randbelow(num_items)
winner = fruit_name_list[random_index]
print(winner)
Output is as follows :
watermelon
function secrets.choice
Functions are similar to functions random.choice
, This function takes the list as the input parameter , And return a random element from the list .
The sample code is as follows :
winner = secrets.choice(fruit_name_list)
print(winner)
Output is as follows :
apple
function secrets.SystemRandom().sample()
Function like random.sample()
, This function takes the input list and the number of returned elements as input parameters , At the same time, the function returns a list of selected random elements .
The sample code is as follows :
winners = secrets.SystemRandom().sample(fruit_name_list,2)
print(winners)
Output is as follows :
['watermelon', 'orange']
This paper introduces the use of Random Library and use Secrets Library to randomly select different implementations of one or more elements from the list , Code examples are given .
Have you learned to waste ?
Reference resources
Official account 《AI The way of algorithm 》, For more AI Algorithm information .