中级农民
- 积分
- 103
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2021-1-14
- 最后登录
- 1970-1-1
|
Here is my first thought here, let me know if there is any doubts.
- outputDict = {} #{1:45, 2:35}
- sessionDict = {} #{'a':[1,110], 'b':[1,110]} keep the latest step of each session
- stepCountDict = {} #{1:2, 2:2}
- #input [a,1,110]
- def step_avg_time(session, step, time_start):
- last_session = sessionDict.get(session)
- #for new session, no change
- if not last_session:
- sessionDict[session] = [step, time_start]
- return outputDict
- time_spend = time_start - last_session[1]
- last_step = last_session[0]
- step_avg = outputDict.get(last_step, 0.0)
- #update the average
- step_avg = step_avg + (time_spend - step_avg) / (stepCountDict.get(last_step,0) + 1)
- outputDict[last_step] = step_avg
- #update the sessionDict
- sessionDict[session] = [step, time_start]
- #update stepCountDict
- stepCountDict[last_step] = stepCountDict[last_step] + 1
- return outputDict
- #call the function
- #inputList: [[a,1,110], [a,2,120], [a,3,150], [b,1,110],[b,2,190],[b,3,220]]
- for i in inputList:
- print(step_avg_time(*i))
复制代码 |
|