It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened,
visit the help center。
已关闭8年。
我有以下数据
Name Year score
A 1996 84
A 1997 65
A 1996 76
A 1998 78
A 1998 65
B 1998 53
B 1996 98
B 1996 83
B 1996 54
我想要输出如下
Name Year max_score
A 1996 84
B 1996 98
如何为该工作编写python map reduce代码?
我可以将NAME和YEAR创建为单个键,并按值计分即可。
但是还有其他方法可以解决这个问题。
假设您所有的年数和分数都是正数:
from collections import defaultdict
mapping = defaultdict( lambda: (0,0) )
with open(datafile) as f:
for line in f:
name,year,score = line.split()
try:
year = int(year)
score = int(score)
except ValueError:
continue
if score > mapping[name][1]:
mapping[name] = year,score
或更简洁,但对错误的鲁棒性较低:
from collections import defaultdict
mapping = defaultdict( lambda: (0,0) )
with open(datafile) as f:
f.readline() #header. Don't need it.
for line in f:
name,year,score = line.split()
if int(score) > mapping[name][1]:
mapping[name] = int(year),int(score)
我是一名优秀的程序员,十分优秀!