结合我的结果,以免创建另一个映射器
Combining my results so not to create another mapper
我正在处理 MapReduce 项目并希望改进我的输出。我正在使用 CSV 文件,其中包含正在发行的机票上的日期,我需要查看哪些颜色的汽车出票最多。第 33 列包含有关车辆颜色和 header "Vehicle color" 的信息。我的 MapReduce 可以工作,但结果可能会更好。第 33 列有空白值和许多写法不同但意思相同的值示例:
WH 和白色,BK 黑色 BLA。我的 MapReducer 将它们计为不同的颜色。最好将它们组合成一个 Key。
sys_stdin = open("Parking_Violations.csv", "r")
for line in sys_stdin:
vehiclecolor = line.split(",")[33].strip()
vehiclecolor = vehiclecolor.strip("Vehicle Color")
if vehiclecolor:
issuecolor = str(vehiclecolor)
print("%s\t%s" % (issuecolor, 1))
dict_color_count = {}
for line in sys_stdin:
line = line.strip()
color, num = line.split('\t')
try:
num = int(num)
dict_color_count[color] = dict_color_count.get(color, 0) + num
except ValueError:
pass
sorted_dict_color_count = sorted(dict_color_count.items(), key=itemgetter(1), reverse=True)
for color, count in sorted_dict_color_count:
print('%s\t%s') % (color, count)
MY Result after MapReduce
BLK 35
WH 21
WHITE 20
BK 16
GRAY 14
WHT 8
BLACK 6
BLA 1
我认为你可以采用的方法是添加一个字典,其中包含到目前为止你的所有颜色变体,并在你计算它们之前替换这些颜色。例如:
# Dictionary with all the colors that you have identified so far
color_dict = {
"BLK":["BLK","BLACK","BLA"],
"WHT":["WHITE","WHT","WHIT"],
}
for line in sys_stdin:
vehiclecolor = line.split(",")[33].strip()
vehiclecolor = vehiclecolor.strip("Vehicle Color")
if vehiclecolor:
testcolor = str(vehiclecolor).upper()
issuecolor = testcolor
for k,v in color_dict.items()
if testcolor in v:
issuecolor = k
print("%s\t%s" % (issuecolor, 1))
从这个意义上说,您将能够用您已知的结果替代和改进您的颜色计数。
如果有帮助请告诉我! :D
我正在处理 MapReduce 项目并希望改进我的输出。我正在使用 CSV 文件,其中包含正在发行的机票上的日期,我需要查看哪些颜色的汽车出票最多。第 33 列包含有关车辆颜色和 header "Vehicle color" 的信息。我的 MapReduce 可以工作,但结果可能会更好。第 33 列有空白值和许多写法不同但意思相同的值示例: WH 和白色,BK 黑色 BLA。我的 MapReducer 将它们计为不同的颜色。最好将它们组合成一个 Key。
sys_stdin = open("Parking_Violations.csv", "r")
for line in sys_stdin:
vehiclecolor = line.split(",")[33].strip()
vehiclecolor = vehiclecolor.strip("Vehicle Color")
if vehiclecolor:
issuecolor = str(vehiclecolor)
print("%s\t%s" % (issuecolor, 1))
dict_color_count = {}
for line in sys_stdin:
line = line.strip()
color, num = line.split('\t')
try:
num = int(num)
dict_color_count[color] = dict_color_count.get(color, 0) + num
except ValueError:
pass
sorted_dict_color_count = sorted(dict_color_count.items(), key=itemgetter(1), reverse=True)
for color, count in sorted_dict_color_count:
print('%s\t%s') % (color, count)
MY Result after MapReduce
BLK 35
WH 21
WHITE 20
BK 16
GRAY 14
WHT 8
BLACK 6
BLA 1
我认为你可以采用的方法是添加一个字典,其中包含到目前为止你的所有颜色变体,并在你计算它们之前替换这些颜色。例如:
# Dictionary with all the colors that you have identified so far
color_dict = {
"BLK":["BLK","BLACK","BLA"],
"WHT":["WHITE","WHT","WHIT"],
}
for line in sys_stdin:
vehiclecolor = line.split(",")[33].strip()
vehiclecolor = vehiclecolor.strip("Vehicle Color")
if vehiclecolor:
testcolor = str(vehiclecolor).upper()
issuecolor = testcolor
for k,v in color_dict.items()
if testcolor in v:
issuecolor = k
print("%s\t%s" % (issuecolor, 1))
从这个意义上说,您将能够用您已知的结果替代和改进您的颜色计数。
如果有帮助请告诉我! :D