2014-01-21 42 views
0

我有一個函數如下:如何計算綜合經驗

edu = sorted(context.education, key=lambda studied: studied["studied_to"], reverse=True) 
    #print edu[0].studied_to 

    job_history = [] 
    for job in context.job_history: 
     if edu[0].fields_of_study in job.industries: 
      from_ = job.from_ 
      to_ = job.to_ 
      industries = job.industries 
      rd = rdelta.relativedelta(to_, from_) # get date difference 
      # I suspect that combined exp calculation would be done here. 
      experience = "{0.years} Years {0.months} Months".format(rd) #get Year - Month format 
      #print experience 
      job_history.append({"job_title": job_title, 
          "company_name": company_name, 
          "from_": from_, 
          "to_": to_, 
          "industries": industries, 
          "experience": experience}) 

    j = sorted(job_history, key=lambda s: s["to_"]) 
    #print j[0]["job_title"] 
    return {"relocate_list": provinces, 
      "disabilities_dict": disabilities, 
      "industries_list": industry_dict, 
      "job_history_sorted": j, 
      "education_sorted": edu} 

我可以從上面的代碼,每個作業的經驗。有沒有辦法來計算組合體驗。
目前,假設用戶在IT行業有多於一個職位供參考,上面的代碼會給我例如1 Years 0 Months1 Years 4 Months

如何計算組合體驗,以上示例爲2 Years 4 Months

我曾嘗試:

rd += rd 

但是,這增加了在同一天,即

1 Years 4 Months + 1 Years 4 Months 

將輸出:

2 Years 8 Months 
+0

我想取兩個「年 - 月」值並將它們加在一起。即如我在上面的問題:用戶有2個entiries:「1年4個月」和「1年0個月」。這是用下面的公式計算出來的:'rd = rdelta.relativedelta(to_,from_) experience =「{0year} Years {0.months} Months」.format(rd)' – Renier

回答

1

爲什麼不創建一個新的變量保存相對deltas並將其顯示在循環外面,例如:

job_history = [] 
total_exp = rdelta.relativedelta() 
for job in context.job_history: 
    if edu[0].fields_of_study in job.industries: 
     from_ = job.from_ 
     to_ = job.to_ 
     industries = job.industries 
     rd = rdelta.relativedelta(to_, from_) # get date difference 
     total_exp += rd 
     job_history.append({"job_title": job_title, 
         "company_name": company_name, 
         "from_": from_, 
         "to_": to_, 
         "industries": industries, 
         "experience": experience}) 
# I suspect that combined exp calculation would be done here. 
experience = "{0.years} Years {0.months} Months".format(total_exp) #get Year - Month format 
#print experience 
+0

我剛剛把'experience =「{0 .years} Years {0.months} Months「.format(total_exp)'在循環內部,以便在附加'」experience「時:experience'不會給我一個錯誤。感謝你的回答,雖然':)' – Renier