简体   繁体   English

在while循环中选择随机项目的行为不符合预期

[英]Choosing random items in a while loop does not behave as expected

I would like to choose exactly n items from the Lists (P1,P2,P3). 我想从列表(P1,P2,P3)中选择正好n个项目。 If they are not already in the result list (LIST) they should be appended/extended. 如果它们还不在结果列表(LIST)中,则应附加/扩展它们。 If the while loop reaches n (here <=3) it should stop. 如果while循环达到n(此处<= 3),则应停止。 But I get results with 4,6 items. 但是我得到了4,6个项目的结果。 Why ? 为什么呢 Thank you. 谢谢。

from random import choice

P1 = ["a", "b","c", "d", "e","f","g","h","i","j"]
P2 = ["a","m","b","n","e","z","h","g","f","j"]
P3 = [("a","b"), ("c","e"), ("g","a"), ("m","j"), ("d","f")]

LIST = []

while len(LIST) <=3:
    c1, c2 = choice(P3)
    d = choice(P1)
    e = choice(P2)
    f = choice(P1)
    g = choice(P2)
    if c1 not in LIST and c2 not in LIST:
        LIST.extend([c1,c2])
        if d not in LIST:
            LIST.append(d)
        if e not in LIST:
            LIST.append(e) 
        if f not in LIST:
            LIST.append(f) 
        if g not in LIST:
            LIST.append(g) 
print LIST

That is because you check that condition at start of each loop. 这是因为您在每个循环开始时都要检查该条件。

At start of the loop condition will be 0, 1, 2 but in the loop you can insert up to 5 new elements, increasing it possibly up to 7. 在循环开始时,条件将为0、1、2,但是在循环中,您最多可以插入5个新元素,可能会增加到7。

The while executes the whole loop, and before starting the next iteration, it checks whether the condition is still true. while执行整个循环,在开始下一次迭代之前,它将检查条件是否仍然为真。

Inside of a single iteration of your loop, you extend the list by up to 6 elements. 在循环的一次迭代中,您最多可以将列表扩展6个元素。

If you really only want 3 elements, that you would have to check the condition every time you appended something, like so: 如果您确实只需要3个元素,则每次添加内容时都必须检查条件,例如:

    while True: 
        c1,c2 = choice(P3)
        d = choice(P1)
        e = choice(P2)
        f = choice(P1)
        g = choice(P2)
        if c1 not in LIST and c2 not in LIST:
            LIST.append(c1)
            if len(LIST) >= 3:
                break
            LIST.append(c2)
            if len(LIST) >= 3:
                break
        if d not in LIST:
            LIST.append(d)
            if len(LIST) >= 3:
                break
        if e not in LIST:
            LIST.append(e) 
            if len(LIST) >= 3:
                break
        if f not in LIST:
            LIST.append(f) 
            if len(LIST) >= 3:
                break
        if g not in LIST:
            LIST.append(g) 
            if len(LIST) >= 3:
                break

You check if there are less/equal than 3 Elements in you list 您检查列表中是否少于/等于3个元素

    while len(LIST) <=3:

after that, you add up to 5 items before you check again. 之后,您最多添加5个项目,然后再次检查。

so if your last loop got 3 items at its begining and it adds up to 5 items, your list can contain up to 8 Elements. 因此,如果您的最后一个循环在开始时有3个项目,并且总共增加了5个项目,则您的列表最多可以包含8个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM