如果值不在 Python 范围内

我正在尝试根据值将数据分为几类。因此,高于 -25 的值被指定为熊级别 0,依此类推。但是当我查看数据时,我可以看到有低于 -25 的值被指定为熊级别 0。所有类别都是如此。


###############################################################

Bear_level = ['high', 'medium-high', 'medium', 'medium-low', 'low', 'very-low']


Level=[]

for value in data_shifted[k]['SCI300max [um]']:

    if value >= -25:

        Level.append(Bear_level[0])

    elif value < -25 and value >= -50:

        Level.append(Bear_level[1])

    elif value < -50 and value >= -75:

        Level.append(Bear_level[2])

    elif value < -75 and value >= -100:

        Level.append(Bear_level[3])

    elif value < -100 and value >= -150:

        Level.append(Bear_level[4])

    else:

        Level.append(Bear_level[5])


Amount = 0

for i in Bear_level:

    for m in range(int(len(Level))):

        if Level[m] ==i:

            Amount += 1

    print(Amount)

    Amount = 0    


for k in data_shifted:

    data_shifted[k]['Bear Level']= Level



data_interp={k:[] for k in progression}   

for k in data_interp:

    data_interp[k]=data_shifted[k][['Chainage [m]', 'Driving Speed [m/s]', 'Latitude', 'Longitude', 'Road temperature [C]', 'Air temperature [C]','Temp corrected Bells2_50','Load Left [kg]','Load Right [kg]', 'Dmax [um]', 'D0 [um]', 'D300 [um]', 'D600 [um]', 'D900 [um]', 'D1200 [um]', 'D1500 [um]', 'SCI300max [um]','SCI300 [um]','SCI300diff [um]', 'SCI600max [um]','SCI600 [um]', 'SCI900max [um]','SCI900 [um]', 'SCI600max-SCI300max [um]', 'SCI900max-SCI600max [um]','speedfilter', 'Bear Level', 'Traffic [ESALs x day]', 'Layer1 Thickness [m]']]

    data_interp[k]= data_interp[k][data_interp[k]['speedfilter']=='Pass']    


我希望有人能够看到问题,因为我迷失了想法。


饮歌长啸
浏览 101回答 1
1回答

慕仙森

你需要翻转你的逻辑。所有值都低于 -25:因此您需要先检查最小值for value in data_shifted[k]['SCI300max [um]']:&nbsp; &nbsp; if value <= -150:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[5])&nbsp; &nbsp; elif value < -100 and value >= -150:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[4])&nbsp; &nbsp; elif value < -75 and value >= -100:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[3])&nbsp; &nbsp; elif value < -50 and value >= -75:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[2])&nbsp; &nbsp; elif value < -25 and value >= -50:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[1])&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[0])而且你真的不需要 and 子句:for value in data_shifted[k]['SCI300max [um]']:&nbsp; &nbsp; if value <= -150:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[5])&nbsp; &nbsp; elif value < -100:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[4])&nbsp; &nbsp; elif value < -75:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[3])&nbsp; &nbsp; elif value <:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[2])&nbsp; &nbsp; elif value < -25:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[1])&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; Level.append(Bear_level[0])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python