Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

python - Using if else statement inside functools.reduce

i have created a function which is working fine , however, i would like to add if else statement inside function.reduce and im not sure if this is achievable. tried researching on func.reduce but there wasnt any information / use-case available on how i want to function to work. would need some advice if this is possible in python or should i go on another approach .

I have created this function which is working as expected, '

def tempJoin(df,targetField,targetTitle,condition,targetResult,otherwise,show=False,lowercase=False):
    case_conditions = list(zip(condition, targetResult))
    product_col = functools.reduce(
        lambda acc, x: acc.when(col(targetField).like(x[0]), F.lit(x[1])),case_conditions,F,).otherwise(otherwise)
    df = df.withColumn(targetTitle, product_col)

    if show:
        df.show(10)
        
    return df;

i am fully aware that i could go with this approach, but i feel this can be further optimise

def tempJoin(df,targetField,targetTitle,condition,targetResult,otherwise,show=False,lowercase=False):
    case_conditions = list(zip(condition, targetResult))
    if lowercase :
        product_col = functools.reduce(
            lambda acc, x: acc.when(lower(col(targetField)).like(x[0]), F.lit(x[1])),case_conditions,F,).otherwise(otherwise)        
    else :
        product_col = functools.reduce(
            lambda acc, x: acc.when(col(targetField).like(x[0]), F.lit(x[1])),case_conditions,F,).otherwise(otherwise)
    
    df = df.withColumn(targetTitle, product_col)

    if show:
        df.show(10)
        
    return df;

what i am trying to achieve is to add if else of 'lowercase=True' , it will triggers within functools itself. something as this shown below

 product_col = functools.reduce(
            if lowercase:
                lambda acc, x: acc.when(lower(col(targetField)).like(x[0]), F.lit(x[1])),case_conditions,F,).otherwise(otherwise)
            else:
                lambda acc, x: acc.when(col(targetField).like(x[0]), F.lit(x[1])),case_conditions,F,).otherwise(otherwise)

is this achievable ?

question from:https://stackoverflow.com/questions/65878495/using-if-else-statement-inside-functools-reduce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So you're looking for an "inline" if statement? Python calls those conditional expressions.

The general syntax is some_value if condition else other_value

So your code would be:

product_col = functools.reduce(lambda ... if lowercase else lambda ...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...