如何从Python中的列表中选择“x”个唯一数字?

时间:2022-08-07 21:39:25

I need to pick out "x" number of non-repeating, random numbers out of a list. For example:

我需要从列表中挑出“x”个非重复的随机数。例如:

all_data = [1, 2, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10, 11, 11, 12, 13, 14, 15, 15]

How do I pick out a list like [2, 11, 15] and not [3, 8, 8]?

如何选择[2,11,15]而不是[3,8,8]的列表?

5 个解决方案

#1


44  

That's exactly what random.sample() does.

这正是random.sample()所做的。

>>> random.sample(range(1, 16), 3)
[11, 10, 2]

Edit: I'm almost certain this is not what you asked, but I was pushed to include this comment: If the population you want to take samples from contains duplicates, you have to remove them first:

编辑:我几乎可以肯定这不是你提出的问题,但我被推荐包括这个评论:如果你想从中获取样本的人口包含重复项,你必须先删除它们:

population = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
population = set(population)
samples = random.sample(population, 3)

#2


3  

Something like this:

像这样的东西:

all_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
from random import shuffle
shuffle(all_data)
res = all_data[:3]# or any other number of items

OR:

from random import sample
number_of_items = 4
sample(all_data, number_of_items)

If all_data could contains duplicate entries than modify your code to remove duplicates first and then use shuffle or sample:

如果all_data可能包含重复的条目而不是修改代码以首先删除重复项,然后使用shuffle或sample:

all_data = list(set(all_data))
shuffle(all_data)
res = all_data[:3]# or any other number of items

#3


2  

Others have suggested that you use random.sample. While this is a valid suggestion, there is one subtlety that everyone has ignored:

其他人建议您使用random.sample。虽然这是一个有效的建议,但每个人都忽略了一个微妙之处:

If the population contains repeats, then each occurrence is a possible selection in the sample.

如果总体包含重复,则每次出现都是样本中可能的选择。

Thus, you need to turn your list into a set, to avoid repeated values:

因此,您需要将列表转换为集合,以避免重复的值:

import random
L = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
random.sample(set(L), x) # where x is the number of samples that you want

#4


1  

Another way, of course with all the solutions you have to be sure that there are at least 3 unique values in the original list.

另一种方式,当然使用所有解决方案,您必须确保原始列表中至少有3个唯一值。

all_data = [1,2,2,3,4,5,6,7,8,8,9,10,11,11,12,13,14,15,15]
choices = []
while len(choices) < 3:
    selection = random.choice(all_data)
    if selection not in choices:
        choices.append(selection)
print choices 

#5


1  

You can also generate a list of random choices, using itertools.combinations and random.shuffle.

您还可以使用itertools.combinations和random.shuffle生成随机选择列表。

all_data = [1,2,2,3,4,5,6,7,8,8,9,10,11,11,12,13,14,15,15]

# Remove duplicates
unique_data = set(all_data)

# Generate a list of combinations of three elements
list_of_three = list(itertools.combinations(unique_data, 3))

# Shuffle the list of combinations of three elements
random.shuffle(list_of_three)

Output:

[(2, 5, 15), (11, 13, 15), (3, 10, 15), (1, 6, 9), (1, 7, 8), ...]

#1


44  

That's exactly what random.sample() does.

这正是random.sample()所做的。

>>> random.sample(range(1, 16), 3)
[11, 10, 2]

Edit: I'm almost certain this is not what you asked, but I was pushed to include this comment: If the population you want to take samples from contains duplicates, you have to remove them first:

编辑:我几乎可以肯定这不是你提出的问题,但我被推荐包括这个评论:如果你想从中获取样本的人口包含重复项,你必须先删除它们:

population = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
population = set(population)
samples = random.sample(population, 3)

#2


3  

Something like this:

像这样的东西:

all_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
from random import shuffle
shuffle(all_data)
res = all_data[:3]# or any other number of items

OR:

from random import sample
number_of_items = 4
sample(all_data, number_of_items)

If all_data could contains duplicate entries than modify your code to remove duplicates first and then use shuffle or sample:

如果all_data可能包含重复的条目而不是修改代码以首先删除重复项,然后使用shuffle或sample:

all_data = list(set(all_data))
shuffle(all_data)
res = all_data[:3]# or any other number of items

#3


2  

Others have suggested that you use random.sample. While this is a valid suggestion, there is one subtlety that everyone has ignored:

其他人建议您使用random.sample。虽然这是一个有效的建议,但每个人都忽略了一个微妙之处:

If the population contains repeats, then each occurrence is a possible selection in the sample.

如果总体包含重复,则每次出现都是样本中可能的选择。

Thus, you need to turn your list into a set, to avoid repeated values:

因此,您需要将列表转换为集合,以避免重复的值:

import random
L = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
random.sample(set(L), x) # where x is the number of samples that you want

#4


1  

Another way, of course with all the solutions you have to be sure that there are at least 3 unique values in the original list.

另一种方式,当然使用所有解决方案,您必须确保原始列表中至少有3个唯一值。

all_data = [1,2,2,3,4,5,6,7,8,8,9,10,11,11,12,13,14,15,15]
choices = []
while len(choices) < 3:
    selection = random.choice(all_data)
    if selection not in choices:
        choices.append(selection)
print choices 

#5


1  

You can also generate a list of random choices, using itertools.combinations and random.shuffle.

您还可以使用itertools.combinations和random.shuffle生成随机选择列表。

all_data = [1,2,2,3,4,5,6,7,8,8,9,10,11,11,12,13,14,15,15]

# Remove duplicates
unique_data = set(all_data)

# Generate a list of combinations of three elements
list_of_three = list(itertools.combinations(unique_data, 3))

# Shuffle the list of combinations of three elements
random.shuffle(list_of_three)

Output:

[(2, 5, 15), (11, 13, 15), (3, 10, 15), (1, 6, 9), (1, 7, 8), ...]