From 2ea753ac1d5f76e78371775dbebd13ade5be4762 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Fri, 19 Mar 2021 11:05:07 -0400 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BD=BF=E7=94=A8=20choices?= =?UTF-8?q?=20=20=E5=87=BD=E6=95=B0=E7=9A=84=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/RandomStringGenerate.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/RandomStringGenerate.py b/tests/RandomStringGenerate.py index c5de637..b158f7b 100644 --- a/tests/RandomStringGenerate.py +++ b/tests/RandomStringGenerate.py @@ -5,7 +5,7 @@ # Link Article - https://www.ossez.com/t/python/13398 import string -from random import choice +import random def random_password(length, printable): @@ -15,7 +15,7 @@ def random_password(length, printable): :param int length: 生成随机字符串的数量 """ - return "".join([choice(printable) for x in range(int(length))]) + return "".join([random.choice(printable) for x in range(int(length))]) if __name__ == "__main__": @@ -39,3 +39,8 @@ for i in range(1, amount + 1): print('') for i in range(1, amount + 1): print(f" 随机字符串 [Ascii Digits String]: {i} - {repr(random_password(number, string.digits))} ") + +# choices Function Test +print('') +my_list = ["apple", "banana", "cherry"] +print(random.choices(my_list, weights=[10, 1, 1], k=12))