Python中的random.choices函數(shù)用法詳解
1. 什么是random.choices函數(shù)?
random.choices
是Python標準庫中random
模塊提供的一個函數(shù),用于從給定的序列中隨機選擇一個值。這個函數(shù)可以用于實現(xiàn)隨機抽樣、按照概率進行選擇等功能。
random.choices(population, weights=None, *, cum_weights=None, k=1)
函數(shù)的參數(shù)解釋如下:
population
:必需參數(shù),指定要進行選擇的序列(可以是列表、元組等)。weights
:可選參數(shù),指定每個元素的權(quán)重(概率)。如果不指定,則默認每個元素的權(quán)重相等。cum_weights
:可選參數(shù),指定累計權(quán)重。如果指定了cum_weights
,則必需省略weights
參數(shù)。k
:可選參數(shù),指定要選擇的元素個數(shù)。默認為1,即只選擇一個元素。
2. random.choices函數(shù)的用法示例
示例1:從列表中隨機選擇一個元素
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] chosen_fruit = random.choices(fruits) print(chosen_fruit)
運行結(jié)果
['grape']
示例2:按照概率從列表中隨機選擇一個元素
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] weights = [0.1, 0.2, 0.3, 0.2, 0.2] chosen_fruit = random.choices(fruits, weights=weights) print(chosen_fruit)
運行結(jié)果
['orange']
示例3:選擇多個元素
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] chosen_fruits = random.choices(fruits, k=3) print(chosen_fruits)
運行結(jié)果
['banana', 'apple', 'watermelon']
示例4:利用cum_weights參數(shù)選擇元素
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] cum_weights = [0.1, 0.4, 0.7, 0.9, 1.0] chosen_fruit = random.choices(fruits, cum_weights=cum_weights) print(chosen_fruit)
運行結(jié)果
['grape']
示例5:選擇多個元素并計算選擇的次數(shù)
import random fruits = ['apple', 'banana', 'orange', 'grape', 'watermelon'] chosen_fruits = random.choices(fruits, k=1000) fruit_counts = {} for fruit in chosen_fruits: if fruit in fruit_counts: fruit_counts[fruit] += 1 else: fruit_counts[fruit] = 1 print(fruit_counts)
運行結(jié)果
{'orange': 334, 'grape': 192, 'apple': 203, 'watermelon': 152, 'banana': 119}
3. 總結(jié)
random.choices
函數(shù)是Python中一個非常有用的函數(shù),可以用于實現(xiàn)隨機抽樣、按照概率進行選擇等功能。通過合理地使用參數(shù),我們可以根據(jù)需求選擇單個或多個元素,并可以對選擇的元素進行計數(shù)等操作。
通過閱讀本文,你應該對random.choices
函數(shù)有了更深入的理解,并可以靈活地將其應用于自己的編程任務中。
4.特別提醒
random.choices 在 k>1 時,也就是選擇的元素個數(shù)大于1時,元素是有可能重復的。要想得到一個不重復的隨機數(shù)列,請自行編寫方法。
附.可用場景
例如:2048這個游戲,每次隨機的值都是2或者4,只有這兩個值。下面是初始化2048棋盤的數(shù)據(jù)的一個函數(shù),里面可以看到咱們使用的就是random.choice來獲取數(shù)組中的隨機兩個值的。
def init(): """ 初始化操作 :return: """ # 隨機生成兩個2或4并防止到棋盤中 for i in range(2): while True: # 棋盤位置 row = random.randint(0, 3) col = random.randint(0, 3) if data[row][col] == 0: # 在數(shù)組重隨機抽取2或4·棋盤數(shù)字 data[row][col] = random.choice([2, 4]) break
注意內(nèi)容
注:
1、random.choice 函數(shù)不能直接用于選擇字典中的隨機鍵值對,因為該函數(shù)是用于從序列中選擇隨機元素的。如果要從字典中選擇隨機鍵值對,可以使用 random.choice(list(dictionary.items())) 的方法來實現(xiàn)。
2、random.choice 函數(shù)不能用于選擇一個隨機的布爾值。該函數(shù)的作用是從給定的序列中隨機選擇一個元素。在布爾值的情況下,你可以使用 random.choice([True, False]) 來隨機選擇一個布爾值。
總結(jié)
到此這篇關(guān)于Python中random.choices函數(shù)用法的文章就介紹到這了,更多相關(guān)Python中random.choices函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python多線程threading join和守護線程setDeamon原理詳解
這篇文章主要介紹了Python多線程threading join和守護線程setDeamon原理詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03Python中卷積神經(jīng)網(wǎng)絡(CNN)入門教程分分享
卷積神經(jīng)網(wǎng)絡(Convolutional Neural Networks, CNN)是一類特別適用于處理圖像數(shù)據(jù)的深度學習模型,本文介紹了如何使用Keras創(chuàng)建一個簡單的CNN模型,并用它對手寫數(shù)字進行分類,需要的可以參考一下2023-05-05Django執(zhí)行源生mysql語句實現(xiàn)過程解析
這篇文章主要介紹了Django執(zhí)行源生mysql語句實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11