如何按条件重新启动循环

时间:2019-06-12 09:07:31

标签: python for-loop

我的代码为:


for i in [data1, data2]:
    pbest_A = i.iloc[:, 0]
    pbest_B = i.iloc[:, 3]

    gbest_score_cycle = i['score'].max()  # max score in cycle
    gbest_score = np.where(gbest_score_cycle > gbest_score, gbest_score_cycle, gbest_score)  # update gbest score
    if gbest_score == gbest_score_cycle:  # row of gbest 
        gbest = i.loc[i['score'].idxmax()] 

    gbest_A = gbest[0]
    gbest_B = gbest[3]

    save_A = []
    save_B = []

    for j in range(5):
        R1 = random.uniform(0,1)
        R2 = random.uniform(0,1)

        Xid_A = New_Xid_A
        Xid_B = New_Xid_B
        Vid_A = New_Vid_A
        Vid_B = New_Vid_B

        New_Vid_A = w*Vid_A + c1*R1*(pbest_A[i]- Xid_A) + c2*R2*(gbest_A - Xid_A)
        New_Vid_B = w*Vid_B + c1*R1*(pbest_B[i] - Xid_B) + c2*R2*(gbest_B - Xid_B)
        New_Xid_A= Xid_A + New_Vid_A
        New_Xid_B= Xid_B + New_Vid_B
        # get result: New_Xid_A, New_Xid_B
        # *** if New_Xid_A > 10 or New_Xid_B > 20, restart this loop (same j in [for j in range(5)])

        save_A.append(New_Xid_A)
        save_B.append(New_Xid_B)

    print(save_A)
    print(save_B)

我正在寻找这种问题。我正在考虑如何在for循环中使用while。现在导致我的问题是当条件符合时,然后重新启动循环(不要追加到save_A和save_B)。有什么办法吗?

3 个答案:

答案 0 :(得分:1)

您可以做的是使用两个while循环,第一个while循环是一个无限循环,只有当内部while循环(直到运行5个)完成时,该循环才会中断并移至for循环中的下一个元素。 / p>

代码如下

for i in [data1, data2]:

    #Outer while loop runs till inner while loop is finished
    while True:
        j = 0
        #Inner while loop
        while j < 5:
            #If condition is met, reset inner counter and break inner while loop
            if New_Xid_A > 10 or New_Xid_B > 20:
                j = 0
                break
            j += 1

        #If inner while loop is successful, break out of infinite loop
        if j == 5:
            break

答案 1 :(得分:1)

“关闭” 函数+ “异常” 方法(pythonic):

def outer():
    # ... all the needed variables

    for i in [data1, data2]:
        pbest_A = i.iloc[:, 0]
        pbest_B = i.iloc[:, 3]

        gbest_score_cycle = i['score'].max()  # max score in cycle
        gbest_score = np.where(gbest_score_cycle > gbest_score, gbest_score_cycle, gbest_score)  # update gbest score
        if gbest_score == gbest_score_cycle:  # row of gbest 
            gbest = i.loc[i['score'].idxmax()]

        gbest_A = gbest[0]
        gbest_B = gbest[3]

        save_A, save_B = [], []

        def inner():
            try:
                for j in range(5):
                    R1 = random.uniform(0, 1)
                    R2 = random.uniform(0, 1)

                    Xid_A = New_Xid_A
                    Xid_B = New_Xid_B
                    Vid_A = New_Vid_A
                    Vid_B = New_Vid_B

                    New_Vid_A = w * Vid_A + c1 * R1 * (pbest_A[i] - Xid_A) + c2 * R2 * (gbest_A - Xid_A)
                    New_Vid_B = w * Vid_B + c1 * R1 * (pbest_B[i] - Xid_B) + c2 * R2 * (gbest_B - Xid_B)
                    New_Xid_A = Xid_A + New_Vid_A
                    New_Xid_B = Xid_B + New_Vid_B

                    # get result: New_Xid_A, New_Xid_B
                    if New_Xid_A > 10 or New_Xid_B > 20:
                        raise ValueError    # terminate current function call

                    save_A.append(New_Xid_A)
                    save_B.append(New_Xid_B)

            except ValueError:
                inner()     # back to the same function with a new call

        inner()

        print(save_A)
        print(save_B)

outer()

答案 2 :(得分:0)

StackOverflow并非旨在学习编程101,但我认为您正在寻找while和break的组合。

break的基本用法

i = 0
while True:
    if i > 10:
       break
    print(i)
    i += 1

输出

0
1
2
3
4
5
6
7
8
9
10