题解 | #牛客网用户练习的平均次日留存率#
牛客网用户练习的平均次日留存率
https://www.nowcoder.com/practice/1f9e2b9be96b4b27a2476f8f12c5d29f
import sys import pandas as pd from datetime import timedelta # fm=pd.read_csv('nowcoder.csv') # fm=fm[['user_id','date']].sort_values(by=['user_id','date']) # fm.date=pd.to_datetime(fm.date).dt.date nowcoder = pd.read_csv('nowcoder.csv') nowcoder=nowcoder[['user_id','date']] nowcoder.user_id=nowcoder.user_id.astype('int') # lt=[3270, 3344, 3415, 3516, 3617, 3689, 3712, 3745, 3760, 3760, 3881, 3890] # nowcoder=nowcoder[nowcoder.user_id==3760] # print(nowcoder.sort_values(by=['user_id','date'])) # pd.set_option('display.max_columns',None) # pd.set_option('display.max_rows',None) # print('================')#how=连接方式 默认全连接 这里两张表都一样 选什么都一个效果 df = pd.merge(nowcoder,nowcoder,on='user_id',how='right',suffixes=['_a','_b']) # print(df.shape[0]) # print(df) df.date_a = pd.to_datetime(df.date_a).dt.date df.date_b = pd.to_datetime(df.date_b).dt.date df = df[(df.date_a+timedelta(days=1))==df.date_b] all_num = nowcoder.user_id.count() again_num = df.user_id.count() # print(df) # print(df.user_id) # print(all_num) # print(again_num) print(round(again_num/all_num,2)) # #题目要求 # #[3270, 3344, 3415, 3516, 3617, 3689, 3712, 3745, 3760, 3760, 3881, 3890] len=12 3760为重复项 # #去重 # #[3270, 3344, 3415, 3516, 3617, 3689, 3712, 3745, 3760, 3881, 3890] len=11 # user_list=[] # for i in range(0,fm.shape[0]-1): # if (fm.iloc[i,0]==fm.iloc[i+1,0]) and \ # ((fm.iloc[i,1]+pd.Timedelta(days=1)==fm.iloc[i+1,1]) o r\ # (fm.iloc[i,0] in user_list)):#去重可删 有被题目要求气到 # user_list.append(fm.iloc[i+1,0]) # r=(len((user_list)))/len((fm.user_id))#id去重就加上set或者用count() # print(round(r,2)) for line in sys.stdin: a = line.split() print(int(a[0]) + int(a[1]))