简体   繁体   English

如何在我的 python function 中将浮点数作为参数传递

[英]How do I pass a float as an argument in my python function

I'm trying to do a simple function;我正在尝试做一个简单的 function; however, I receive this error: 'float' object is not subscriptable.但是,我收到此错误:'float' object 不可下标。

I know the float is causing the error, i'm just not sure how to handle this.我知道浮动导致错误,我只是不确定如何处理。 Thanks in advance for any help.提前感谢您的帮助。

# prediction probabilities
probs = [0.886,0.375,0.174,0.817,0.574,0.319,0.812,0.314,0.098,0.741,
         0.847,0.202,0.31,0.073,0.179,0.917,0.64,0.388,0.116,0.72]

# threshold value
thresh = 0.5

def predict(x,y):
    for i in x:
        if i >= y[0]:
            z.append(1)
        else:
            z.append(0)
            return(z)

predict(probs,thresh) 

Yes, because y , which is tresh which is set to 0.5 , a float value.是的,因为ytresh设置为0.5的浮点值。 Did you mean x[0] ?你的意思是x[0]吗? Because this is a list.因为这是一个列表。

Based on our conversation in the comments above, I think you are looking for something like:根据我们在上面评论中的对话,我认为您正在寻找类似的东西:

def predict(x, y):
    z = []
    for i in x:
        if i >= y:
            z.append(1)
        else:
            z.append(0)
    return z

The key things to note are 1) creating z so that you can append to it 2) not having the return call inside the else body (or else it returns the very first time you end up in that body and stops going through all the elements of x ).需要注意的关键事项是 1) 创建z以便您可以对其进行 append 2) 在else主体中没有return调用(否则它会在您第一次进入该主体并停止遍历所有元素时返回x )。

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

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