有三个文件, 分别是 ad clike user, 其中:
ad代表哪个用户投放了哪个广告
clike代表每个广告每天的花销
user代表用户和用户名的对应关系
文件内容如下:
# cat ad
car user_01
dacar user_01
youxi user_02
shouyou user_02
yeyou user_02
shuiguo user_03
# cat click
car 20160113 102
dacar 20160113 109
youxi 20160113 103
shouyou 20160113 106
yeyou 20160113 116
shuiguo 20160113 111
shouyou 20160113 123
yeyou 20160113 121
shuiguo 20160113 105
dacar 20160113 108
youxi 20160113 112
shouyou 20160113 121
shuiguo 20160113 109
shouyou 20160113 112
yeyou 20160113 101
shuiguo 20160113 100
car 20160113 102
dacar 20160113 109
youxi 20160113 103
shouyou 20160113 106
yeyou 20160113 116
shuiguo 20160114 111
shouyou 20160114 123
yeyou 20160114 121
shuiguo 20160114 105
dacar 20160114 108
youxi 20160114 112
shouyou 20160114 121
shuiguo 20160114 109
shouyou 20160114 112
yeyou 20160114 101
shuiguo 20160114 100
# cat user
user_01hailei
user_02 cuikai
user_03 duanhuan
问题: 求哪个用户花的广告费最高?
ad文件中,用户名是有重复的,需要按用户把广告汇总。
代码实现来说,也没什么特别的,读取3个文件,把内容加载到python的数据结构中,然后求和,排序,打印即可。
为了突出重点功能,代码中不加验证性的代码。
# -*- coding: utf-8 -*- from collections import defaultdict click = defaultdict(list) ads = defaultdict(list) users = {} # 将3个文件存储到上面的3个字典中 with open("click", "r") as f: for i in f: v = i.strip().split() click[v[0]].append(int(v[2])) with open("user", "r") as f: for i in f: v = i.strip().split() users[v[0]] = v[1] # ads数据结构为:defaultdict(<type ‘list‘>, {‘user_03‘: [‘shuiguo‘], ‘user_02‘: [‘youxi‘, ‘shouyou‘, ‘yeyou‘], ‘user_01‘: [‘car‘, ‘dacar‘]}) with open("ad", "r") as f: for i in f: v = i.strip().split() ads[v[1]].append(v[0]) # 将分属通一个用户的广告费的和添加到可以为username的字典中,值为列表 moneys = defaultdict(list) for ad in ads: for g in click: if g in ads[ad]: moneys[ad].append(sum(click[g])) # 将用户的广告费求和,倒序排列后,第一个即为所求用户 um = [] for m in moneys: um.append((m, sum(moneys[m]))) sorted_um = sorted(um, key=lambda x: x[1], reverse=True)[0] print sorted_um[0], users[sorted_um[0]], sorted_um[1]
答案:
user_02 cuikai 2030
本文出自 “烛影摇红” 博客,请务必保留此出处http://gccmx.blog.51cto.com/479381/1736229
原文地址:http://gccmx.blog.51cto.com/479381/1736229