gpt4 book ai didi

python - Map减少存储 'Nan',同时计数大对象

转载 作者:行者123 更新时间:2023-12-01 06:01:35 28 4
gpt4 key购买 nike

map :

function () { 
emit(this.thread,
{max_year:this.date.getFullYear(),
min_year:this.date.getFullYear(),
max_month:this.date.getMonth(),
min_month:this.date.getMonth(),count:1});

};

Reduce:
function (key, values) {
max_year= values[0].max_year;
min_year = values[0].min_year;
max_month= values[0].max_month;
min_month = values[0].min_month;
var sum = 0;
if (values.length > 1){
for(i in values){
if(values[i].max_year > max_year){
max_year = values[i].max_year;
};
if(values[i].min_year < min_year){
min_year = values[i].min_year;
};
if(values[i].max_month > max_month){
max_month = values[i].max_month;
};
if(values[i].min_month < min_month){
min_month = values[i].min_month;
};
sum+=values[i].count
};
};

return {"max year":max_year, "min year":min_year, "max month":max_month, "min month":min_month, "No of posts": sum};
}
};

输出:

{u'_id': u'Sujet  Top 5 TED POST', u'value': {u'No of posts': 8.0, u'min month': 0.0, u'max month': 6.0, u'max year': 2011.0, u'min year': 2010.0}}
{u'_id': u'Sujet Top 5 des meilleurs guitaristes de lhistoire du Rock', u'value': {u'No of posts': 42.0, u'min month': 2.0, u'max month': 10.0, u'max year': 2011.0, u'min year': 2009.0}}
{u'_id': u'Sujet Top ALEJANDRO GONZALEZ INARRITU', u'value': {u'No of posts': 29.0, u'min month': 0.0, u'max month': 9.0, u'max year': 2011.0, u'min year': 2008.0}}
{u'_id': u'Sujet Top ANDY et LARRY WACHOWSKY', u'value': {u'No of posts': 40.0, u'min month': 0.0, u'max month': 11.0, u'max year': 2011.0, u'min year': 2008.0}}
{u'_id': u'Sujet Top BRYAN SINGER', u'value': {u'No of posts': 50.0, u'min month': 0.0, u'max month': 11.0, u'max year': 2011.0, u'min year': 2006.0}}
{u'_id': u'Sujet Top Cinma 2010', u'value': {u'No of posts': nan, u'min month': None, u'max month': None, u'max year': None, u'min year': None}}
{u'_id': u'Sujet Top Cinma 2011', u'value': {u'No of posts': nan, u'min month': None, u'max month': None, u'max year': None, u'min year': None}}

正如您所看到的,对于某些字段(“帖子数量”),它打印“Nan”,而对于其他字段则打印“non”。当我 MapReduce 只是为了计算帖子数量而不尝试处理时间戳时,不会发生这种情况。我还注意到,当“帖子数量”很大(大约 1000 左右)时,Nan 就会被打印。此外,如果没有“计数”和“求和”,对最大年份、最小年份和月份的所有操作都很好。 谢谢。

最佳答案

您的reduce函数需要返回一个与emit()第二个参数格式相同的值——由于MongoDB Map-Reduce的工作方式,reduce函数的结果可能是再次传入减少。我怀疑这就是 nanNone 的来源。具体来说,您只需要调整从reduce返回的对象中的键名称:例如,您应该使用max_year,而不是“maxyear”(在reduce中) >.

有关编写正确的映射和归约函数的更多信息,请参阅 the MongoDB Map-Reduce documentation .

关于python - Map减少存储 'Nan',同时计数大对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10137640/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com