可重复示例:
import random #必须导入随机函数
exam_choice_list = [random.choice(exam_msg_list) for _ in range(3)]
- random.choice(exam_msg_list) for _ in range(3) 循环3次,即在exam_msg_list中取3个可重复的元素,并将其结果赋给exam_choice_list列表。
不重复示例:
import random #必须导入随机函数
exam_choice_list = random.sample(exam_msg_list, 3)
- random.sample()函数可以从序列中选择n个随机且独立的元素,即不重复。