使用条件语句在多个forloops中中断并返回

2024-09-26 22:55:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个很长的代码,里面有多个for循环列表。没有什么不对的计算。它确实取得了预期的结果。列表的代码和结构工作得很好。问题是当它运行时,我定义了一个中断,当某个条件匹配时。但它不会在第一次运行时中断,而是继续运行并遍历第一个循环的range函数中的所有值。我想实现返回一个真值,当条件匹配,并停止,将不会继续增长,根据范围值在第一个循环。你知道吗

我会解释代码是如何工作的!你知道吗

代码: 第一部分是一致的,是输入

import math
import numpy as np


Ned = -500
fcd = 20
fyd = 435
E = 2e5
h = 200
cb = 35
ct = 35
ca = 35
b= 150
y = 12
d = h - cb
ds = ct
a = 25
yb = 8
ecu = 0.0035
rebarnumber = math.floor((b-(2*cb+2*yb+y))/a)
PI_Y2_4 = int(math.pi/4*(y)**2)
disc = []
dis = []
Asi = []
Asci = []
Esc = []
Esci = []
Sc = []
Sci =[]
#############################
# Calculation starts here
#############################

for n in range(0,10):         # <------- First for-loop
    cbb = cb + yb + y/2
    ctt = ct + yb + y/2

    if  0 < n <= rebarnumber:
        Asi.append(PI_Y2_4)
        dis.append( h - cbb)
        Asci.append(PI_Y2_4)
        disc.append( ctt )
    if  rebarnumber < n <= (2 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + ca)
    if (2*rebarnumber) < n <= (3 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - 2*ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + 2*ca)
    if (3*rebarnumber) < n <= (4 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - 3*ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + 3*ca) 
    if (4*rebarnumber) < n <= (5 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - 4*ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + 4*ca)  
    if (5*rebarnumber) < n <= (6 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - 5*ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + 5*ca)
    if (6*rebarnumber) < n <= (7 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - 6*ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + 6*ca)
    if (7*rebarnumber) < n <= (8 * rebarnumber):
        Asi.append(PI_Y2_4)
        dis.append( h - cbb - 7*ca)
        Asci.append(PI_Y2_4)
        disc.append(cbb + 7*ca)

    for i in range(0,len(dis)):
        Esc.insert(i, dis[i])
        Esci.insert(i, disc[i])
        Sc.insert(i, dis[i])
        Sci.insert(i, disc[i])

        for x in np.linspace(1,h,10000):    # <-------- Second for-loop
            for k, _ in enumerate(Esc):
                try:    
                    if x < dis[k]:
                        Esc[k]=( ecu/x*(dis[k]-x) )
                    else:
                        Esc[k]=(- ecu/x*(x-dis[k] ) )

                    if x < disc[k]:
                        Esci[k]=( -ecu/x*(x-disc[k]) )
                    else:
                        Esci[k]=(- ecu/x*(x-disc[k]) )
                except (ZeroDivisionError, RuntimeWarning):
                    Esc[k]=( 0 )
                    Esci[k]=( 0 )       

            for k, _ in enumerate(Sc):       # <-------- Third for-loop
                ss = Esc[k]*E
                if ss <= -fyd:
                    Sc[k]= -fyd
                elif ss >= -fyd and ss < 0:
                    Sc[k]=ss
                else:
                    Sc[k]=min(ss,fyd)

            for k, _ in enumerate(Sci):
                sci = Esci[k]*E
                if sci <= -fyd:
                    Sci[k]= -fyd
                elif sci >= -fyd and sci < 0:
                    Sci[k]=sci
                else:
                    Sci[k]=min(sci,fyd)

            FS = 0
            FSC = 0
            for a, _ in enumerate(Sc):
                FS += Sc[a]*Asi[a]
                FSC+=Sci[a]*Asci[a]

            MS = 0        
            MSC = 0
            for m, _ in enumerate(Sc):
                MS += Sc[a]*Asi[a]*(dis[m]-h/2)
                MSC+= Sci[a]*Asci[a]*(h/2-disc[m])

            Nrd = 0
            Mrd = 0
            Nrd = int((-0.8*x*b*fcd+FSC+FS)/1000)
            Mrd = (0.8*x*b*fcd*(h/2-0.4*x)+MS-MSC)/1000000

            if 0 <= (float(Nrd) - Ned) <= 1:
                print(Nrd, x, Asi)
                break
        break    

它是如何工作的?

第一个for循环创建索引为0的列表,即[value1],第二个for循环生成x值增量(范围),第三个for循环创建列表依赖于第一个创建的列表[value1]。然后Nrd值根据x值的增量确定。如果满足条件0 <= (float(Nrd) - Ned) <= 1:,则计算将停止并返回Nrd值。如果不匹配,则返回并从第一个for循环获取索引1,[value1, value2]被创建,如果条件满足,则再次到达Nrd,否则将中断,直到匹配为止。你知道吗

我的问题是当代码运行时,我会得到那些输出。你知道吗

 Nrd       x             Asi
---------------------------------------------------
-499 181.84938493849384 [113]
-499 162.36533653365336 [113, 113]
-499 147.3990399039904 [113, 113, 113]
-499 137.48784878487848 [113, 113, 113, 113]
-499 130.72117211721172 [113, 113, 113, 113, 113]
-499 126.10391039103911 [113, 113, 113, 113, 113, 113]
-499 122.7006700670067 [113, 113, 113, 113, 113, 113, 113]
-499 120.01390139013901 [113, 113, 113, 113, 113, 113, 113, 113]
-499 119.71537153715371 [113, 113, 113, 113, 113, 113, 113, 113, 113]

以上输出都是真正的多重解决方案。但我想在第一场比赛中停止(突破),而不是给出所有的解决方案。你知道吗

我已经使用了return True,但是它在这里并没有真正起作用,当它抱怨没有功能时。你知道吗

这里的第二个问题是第二个for循环for x in np.linspace(1,h,10000):我真的很想用许多小数来运行它以获得最佳结果,但是它会减慢速度并需要很长时间来计算。有没有办法加快速度?你知道吗

也许将上述代码线定义为函数会更有效。你知道吗


Tags: inforifpicadiscscdis
1条回答
网友
1楼 · 发布于 2024-09-26 22:55:34

最简单、最直接的解决方案是将所有代码移到函数中

 def calculate_results(...args...): # Bad name, use a more suitable one
     ...
     for n in range(0, 10): # <   - First for-loop
         ...
         for x in np.linspace(1,h,10000):    # <     Second for-loop
         ...
         if 0 <= (float(Nrd) - Ned) <= 1:
            return Nrd, x, Asi

然后调用函数

Nrd, x, Asi = calculate_results(...)
print(Nrd, x, Asi)

使用一个真正描述函数本质的函数名,并且只做函数中的一件事。然后return语句清楚地表明这个任务现在已经完成了。你知道吗

相关问题 更多 >

    热门问题