python如何从一个列表中取出数字_您如何从Python列表中选择“ x”个唯一数字?...

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

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

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

我该如何选择像[2, 11, 15]这样的列表,而不是[3, 8, 8]?

George asked 2020-07-03T14:28:56Z

6个解决方案

74 votes

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

>>> random.sample(range(1, 16), 3)

[11, 10, 2]

编辑:我几乎可以肯定这不是您要的,但是我被迫加入此注释:如果要从中进行采样的总体中包含重复项,则必须先将其删除:

population = [1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]

population = set(population)

samples = random.sample(population, 3)

Sven Marnach answered 2020-07-03T14:29:18Z

4 votes

像这样:

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

要么:

from random import sample

number_of_items = 4

sample(all_data, number_of_items)

如果all_data可能包含重复项,则需要修改代码以先删除重复项,然后使用shuffle或sample:

all_data = list(set(all_data))

shuffle(all_data)

res = all_data[:3]# or any other number of items

Artsiom Rudzenka answered 2020-07-03T14:29:47Z

4 votes

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

如果总体中包含重复项, 那么每次发生都是可能的 样本中的选择。

因此,您需要将列表变成一个集合,以避免重复的值:

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

inspectorG4dget answered 2020-07-03T14:30:16Z

1 votes

当然,对于所有解决方案,另一种方式是必须确保原始列表中至少有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

Joe answered 2020-07-03T14:30:36Z

1 votes

您还可以使用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)

输出:

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

riza answered 2020-07-03T14:31:00Z

0 votes

import random

fruits_in_store = ['apple','mango','orange','pineapple','fig','grapes','guava','litchi','almond']

print('items available in store :')

print(fruits_in_store)

my_cart = []

for i in range(4):

#selecting a random index

temp = int(random.random()*len(fruits_in_store))

# adding element at random index to new list

my_cart.append(fruits_in_store[temp])

# removing the add element from original list

fruits_in_store.pop(temp)

print('items successfully added to cart:')

print(my_cart)

Output:

items available in store :

['apple', 'mango', 'orange', 'pineapple', 'fig', 'grapes', 'guava', 'litchi', 'almond']

items successfully added to cart:

['orange', 'pineapple', 'mango', 'almond']

Shashank Rautela answered 2020-07-03T14:32:16Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值