python如何再次绘制与另一个变量相同的变量

python如何再次绘制与另一个变量相同的变量,python,random,while-loop,draw,Python,Random,While Loop,Draw,我有两份清单,基本上相同: import random A = [ 0, 10, 20, 30, 40 ] B = [ 0, 10, 20, 30, 40 ] drawA =(random.choice(A)) drawB =(random.choice(B)) # want to exclude the number drawn in drawA 如果drawB==drawA,如何让python再次绘制 或者,我如何从列表B中提取一个数字,排除列表a中已经提取的数字?在查找随机数时,只需

我有两份清单,基本上相同:

import random

A = [ 0, 10, 20, 30, 40 ] 
B = [ 0, 10, 20, 30, 40 ]
drawA =(random.choice(A))
drawB =(random.choice(B)) # want to exclude the number drawn in drawA
如果
drawB==drawA
,如何让python再次绘制


或者,我如何从列表B中提取一个数字,排除列表a中已经提取的数字?

在查找随机数时,只需从B中排除drawA的值即可

drawB = random.choice(filter(lambda num: num != drawA, B))

继续循环,直到得到所需的结果

import random

A = [ 0, 10, 20, 30, 40 ] 
B = [ 0, 10, 20, 30, 40 ]

drawA = random.choice(A)
number = random.choice(B)
while number == drawA:
    number = random.choice(B)

drawB = number

在没有元素的修改数组中搜索

import random

A = [ 0, 10, 20, 30, 40 ] 
B = [ 0, 10, 20, 30, 40 ]
drawA =(random.choice(A))
drawB =(random.choice([x for x in B if x != drawA]))

首先,我们可以为B创建一个随机数生成器:

def gen_B():
    while True:
         yield random.choice(B)
然后选择第一个不是用于以下项的值:

drawB = next(x for x in gen_B() if x != drawA)
或者,您可以使用:

import itertools
next(x for x in (random.choice(B) for _ in itertools.count()) if x != drawA)

A
B
总是一样的吗?为什么不直接使用
random.shuffle
后跟
list.pop
?或者,
drawA,drawB=random.sample(A,2)
。@PeterWood:只有当
A==B
@EricDuminil
A
B
是相同的,OP没有说明其他情况。模块还提供了哪些选项?@PeterWood我得到的采样可能有效,但如果两个列表不同怎么办。