在python中从一个范围随机样本中排除一组元组



我从两组不同的数字中随机抽取N个元组,如下所示:

set1 = [list(range(10))]
set2 = [list(range(10,20))]
c1 = np.random.choice(set1,N) #ex [9,3,7,8]
c2 = np.random.choice(set2,N) #ex [15,13,19,12]
tuples = np.concatenate([c1,c2],axis=1) #ex [[9,15],[9,19],[3,12]]

对于下一次迭代,我想再次采样c1,c2,但不包括我已经拥有的唯一元组。这些数字可以再次出现,但不是(number1,number2)的相同组合。理想情况下是这样的:

new_tuples = np.random.choice([set1,set2],exclude=tuples)

可以用np来检验。但我希望这是一种更有效的方法。

编辑:事先获得所有可能的组合将是非常昂贵的。

在asker注释之后,我修复了代码:

import numpy as np
import time
begin = time.time()
N = 4
set1 = list(range(10**6))
set2 = list(range(10**6, 20**6))
c1 = np.random.choice(set1,N) #ex [9,3,7,8]
c2 = np.random.choice(set2,N)
tuples = set(list(zip(c1, c2)))
print(tuples)
c1 = np.random.choice(set1,N) #ex [9,3,7,8]
c2 = np.random.choice(set2,N)
new_tuples = set([(n1, n2) for (n1, n2) in list(zip(c1, c2)) if (n1, n2) not in tuples][0:4])
print(new_tuples)
print(tuples | new_tuples)
print(time.time() - begin)

注释一步一步解释。测试了200亿次,13秒就回来了!输出获得:

##{(315090, 13207382), (175935, 7922219), (249258, 59598185), (45681, 27246043)}

{(446782, 45042493), (122963, 12794175), (388061, 20418275), (328064, 48911155)}{(315090, 13207382), (175935, 7922219), (328064, 48911155), (446782, 45042493), (249258, 59598185), (45681, 27246043), (122963, 12794175), (388061, 20418275)}12.917975664138794

最新更新