程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to randomly select an element from a list in Python

編輯:Python

1. introduction

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 . :)

2. Take a chestnut

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'
]

3. Use Random library

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 .

3.1 Random subscript

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

3.2 Select a single element at random

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

3.3 Select multiple elements at random

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']

4. Use Secrets library

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 .

4.1 Random subscript

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

4.2 Select a single element at random

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

4.3 Select multiple elements at random

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']

5. summary

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved