如何从 python 中的数组中随机 select 元素?

How to randomly select elements from an array in python?

我需要从列表中随机 select 个元素。目前,我有时 select 原始列表中的元素的副本过多,例如:

Original List: [0, 1, 2, 3, 4]

3 Randomly Selected Elements: [4, 4, 4]

如果原始列表中只有 1 个,我不想 select编辑多个 4。

我应该怎么做才能不获取比第一个数组中存在的值更多的副本?

一个解决方案是在 select 删除元素时从原始列表中删除这些元素。

import random 
original_list = [1, 2, 2, 3, 3, 3]
number_of_random_selections = 4
random_selections = []

for i in range(0, len(original_list)):
    random_index = random.randint(0, number_of_random_selections)
    random_selection = original_list[random_index]
    random_selections.append(random_selection)
    original_list.remove(random_selection)

print(random_selections)