Python Random ရွေးချယ်မှုများ() နည်းလမ်း
ဥပမာ
အချက် ၁၄ ချက်ပါသော စာရင်းကို ပြန်ပေးပါ။
စာရင်းတွင် သတ်မှတ်ထားသော စာရင်းတစ်ခုမှ တန်ဖိုးများကို ကျပန်းရွေးချယ်မှုများ ပါဝင်သင့်ပြီး အခြားနှစ်ခုထက် "apple" ကို ရွေးချယ်ရန် ဖြစ်နိုင်ခြေ 10 ဆ ပိုများသင့်သည်-
import random
mylist = ["apple", "banana", "cherry"]
print(random.choices(mylist,
weights = [10, 1, 1], k = 14))
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
နည်းလမ်း သည် choices()
သတ်မှတ်ထားသော အစီအစဥ်မှ ကျပန်းရွေးချယ်ထားသော ဒြပ်စင်နှင့်အတူ စာရင်းတစ်ခုကို ပြန်ပေးသည်။
အလေးချိန်ဘောင် သို့မဟုတ် cum_weights ကန့်သတ်ချက်များဖြင့် ရလဒ်တစ်ခုစီ၏ ဖြစ်နိုင်ခြေကို ချိန်ဆနိုင်သည်။
sequence သည် string တစ်ခု၊ range တစ်ခု၊ list တစ်ခု၊ tuple သို့မဟုတ် အခြား sequence အမျိုးအစားတစ်ခု ဖြစ်နိုင်သည်။
အထားအသို
random.choices(sequence, weights=None, cum_weights=None, k=1)
ကန့်သတ်တန်ဖိုးများ
Parameter | Description |
---|---|
sequence | Required. A sequence like a list, a tuple, a range of numbers etc. |
weights | Optional. A list were you can weigh the possibility for each value. Default None |
cum_weights | Optional. A list were you can weigh the possibility for each value, only
this time the possibility is accumulated. Example: normal weights list: [2, 1, 1] is the same as this cum_weights list; [2, 3, 4]. Default None |
k | Optional. An integer defining the length of the returned list |