简体   繁体   English

Python 具有多个 arguments 的函数

[英]Python Functions with multiple arguments

I have written the following code我写了以下代码

def createv(dsn,vname,benchdsn,benchvar):
    #global grp
    for t in range(len(dsn)):
        w=dsn[vname].iloc[t]
        #print(w)
        for index in range(len(benchdsn)):
            #print(index)
            z = benchdsn[benchvar].iloc[index]
            #print(z)
            if w <= z:
                grp = z
                print(grp)
                break     
            else:
                continue
    return grp

recent_sample['carvaluegrp2']= createv(recent_sample,'CarValue',df6,'carvalueU')

The issue is that for the newly created variable carvaluegrp2, all values are the same - which is the last value of CarValue from recent_sample.问题在于,对于新创建的变量 carvaluegrp2,所有值都是相同的 - 这是来自 recent_sample 的 CarValue 的最后一个值。 I tried to use map() and apply() functions, but I am getting syntax errors.我尝试使用 map() 和 apply() 函数,但出现语法错误。 All I want is to have appropriate value - which the function is returning for each record of CarValue - as a value in the variable carvaluegrp2.我想要的只是具有适当的值 - function 为 CarValue 的每条记录返回 - 作为变量 carvaluegrp2 中的值。

You need to append your result to a running list like this:您需要将 append 您的结果添加到这样的运行列表中:

def createv(dsn,vname,benchdsn,benchvar):
    result = []
    for t in range(len(dsn)):
        w=dsn[vname].iloc[t]
        #print(w)
        for index in range(len(benchdsn)):
            #print(index)
            z = benchdsn[benchvar].iloc[index]
            #print(z)
            if w <= z:
                grp = z
                print(grp)
                break     
            result.append(grp)
    return result

Also, you never need else: continue .此外,您永远不需要else: continue It's the same as having the if statement without an else.这与没有 else 的 if 语句相同。

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

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