简体   繁体   English

处理大量数据的 if else 语句以外的有效方式 [重复]

[英]efficient way other than the if else statement to process a lot of data [Duplicated]

i need to compare a lot of data, what i'm doing right now is using if else statement but it will cost thousands of line, is there any efficient way to do it properly ?.我需要比较大量数据,我现在正在做的是使用 if else 语句,但它会花费数千行,有没有什么有效的方法可以正确地做到这一点? this is the code :这是代码:

// the data will look like this
selected_data_jan = [20]
year1_jan = [30]
// the data is a sum from several lists

if selected_data_jan[0] < year1_jan[0] :
    result = selected_data_jan[0] - year1_jan[0]
    condition_decrease.append("value decrease this year" +result+ " cases")
elif selected_data_jan[0] > year1_jan[0] :
    result = selected_data_jan[0] - year1_jan[0]
    condition_increase.append("value increase this year" +result+ " cases")
    if selected_data_jan[0] > year1_jan[0] *2:
        result = selected_data_jan[0] - year1_jan[0]*2
        condition_increase.clear()
        condition_increase.append("value increased 2-fold this year, as much" +result+ "cases")
else:
    result = selected_data_jan[0] - year1_jan[0]
    condition_equal.append("value equal this year")

//after this selected_data_jan will be compared with year2 and year3
//and there will be selected_data_feb,mar,apr...des too

the selected_data_jan (monthly) and year1_jan (yearly) got the value form dataframe and turn it into a list, and the list value will be like this : selected_data_jan (monthly) 和year1_jan (yearly) 从数据year1_jan获取值并将其转换为列表,列表值将如下所示:

selected_data_jan_raw = [6,3,5,6]
// then ill sum the list
selected_data_jan = sum(selected_data_jan_raw)

i want to do it efficiently and fewer lines, if I did it the way I do it now it would take thousands of lines, how can i make it work the way i want ?.我想高效地完成它,更少的行,如果我按照我现在的方式来做,将需要数千行,我怎样才能让它按照我想要的方式工作? thanks in advance.提前致谢。

You can create something like this to repeat the process.您可以创建类似的内容来重复该过程。 I am not sure how your data is.我不确定你的数据如何。 I am making assumptions.我在做假设。 Once I know what your data will look like, the solution can be tweaked.一旦我知道您的数据会是什么样子,就可以调整解决方案。

def check_result(sel_mon, year):
    if sel_mon < year:
        diff = sel_mon - year
        cond = 'de'
        results = "value decrease this year" + str(diff)+ " cases"
    elif sel_mon > (year * 2):
        cond = 'i2'
        diff = sel_mon - (year * 2)
        results = "value increase 2-fold this year, as much" +str(diff)+ " cases"
    elif sel_mon > year:
        cond = 'i1'
        diff = sel_mon - year
        results = "value increase this year" +str(diff)+ " cases"
    else:
        cond = 'eq'
        diff = sel_mon - year
        results = "value equal this year"

    return cond, results


selected_data = [10,20,30,40,50,60,70,80,90,100,110,120]
years = [40,50,60,80]

for yrs in years:
    for mon in selected_data:
        condition, cond_result = check_result(mon,yrs)

        #for each year, you can send the selected data
        #assumption is selected data is constant for various years
        
        #now you have the type of result
        #de = decreased, i1 = increased, i2 = 2-fold increase, eq = equal
        #you also have the result string to work with

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

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