从与另一个列表没有共同元素的总体中生成随机样本列表

问题描述 投票:0回答:2

我希望从一个列表中抽样元素,以使该元素中的任何一个都不包含在另一个指定元素列表中。我希望继续生成新样本,直到生成不相交的样本为止。下面的代码是我想到的,但是只要有一个相交的初始样本,它就无法工作,它会陷入无限循环,并且打印结果表明所有生成的样本都是相同的。

import random 
unique_entities=['100','1001','10001','100001','11111']
pde_fin= ['2151', '2146', '2153', '2135', '2158', '2160', '2137', '2169', '2147', '2015', '2022', '2173', '2028', '2014', '2018', '2009', '1140', '1085', '1136', '1132', '1007', '1080', '1078', '1131', '1106', '1164', '1092', '1108', '1118', '1045', '1051', '1006','1001']
random_entities=random.sample(unique_entities,3) #choses 5 unique entities 
while(not(set(random_entities).isdisjoint(pde_fin))):
       random_entites=random.sample(unique_entities,5)
       print(random_entities,"random_entites")

print(unique_entities)

您能帮我了解出什么问题了吗?

python list intersection sampling
2个回答
0
投票

您可以在进行采样之前过滤unique_entities。在数学上,在随机性方面,之前或之后的过滤是相同的。

unique_entities=['100','1001','10001','100001','11111']
pde_fin= ['2151', '2146', '2153', '2135', '2158', '2160', '2137', '2169', '2147', '2015', '2022', '2173', '2028', '2014', '2018', '2009', '1140', '1085', '1136', '1132', '1007', '1080', '1078', '1131', '1106', '1164', '1092', '1108', '1118', '1045', '1051', '1006','1001']
unique_entities_unique = [i for i in unique_entities if not i in pde_fin]
random_entities=random.sample(unique_entities_unique,3)
print(random_entities,"random_entites")

0
投票

random_entites=random.sample(unique_entities,5)行有两个问题:

  • [首先,有一个错字,您写的是random_entites而不是random_entities
  • 第二,您从unique_entities中抽取了5个元素的样本,而该样本恰好总共只包含5个元素。因此,样本始终包含元素'1001',这也是pde_fin中的一个元素。

这里是该程序的有效版本,其中包括一些其他调整:

import random

unique_entities = ['100', '1001', '10001', '100001', '11111']
pde_fin = ['2151', '2146', '2153', '2135', '2158', '2160', '2137', '2169', '2147', '2015', '2022', '2173', '2028',
           '2014', '2018', '2009', '1140', '1085', '1136', '1132', '1007', '1080', '1078', '1131', '1106', '1164',
           '1092', '1108', '1118', '1045', '1051', '1006', '1001']

sample_size = 3

random_entities = set(random.sample(unique_entities, sample_size))
print(f"{random_entities=}")
while not random_entities.isdisjoint(pde_fin):
    random_entities = set(random.sample(unique_entities, sample_size))
    print(f"{random_entities=}")

print(f"Result: {random_entities}")
© www.soinside.com 2019 - 2024. All rights reserved.