作者CKPILOT (新年快乐!)
看板R_Language
标题Re: [问题] 比对两个list
时间Fri Nov 6 14:21:01 2015
#BAD example: without memory preallocation
intersection <- names(a.list)[names(a.list) %in% names(b.list)]
result <- as.list(NULL)
for(i in intersection){
result[[i]] <- sum(table(a.list[[i]][a.list[[i]] %in% b.list[[i]]]))
}
#Fixed:
intersection <- names(a.list)[names(a.list) %in% names(b.list)]
result <- vector("list", length(intersection))
names(result) <- intersection
for(i in intersection){
result[[i]] <- sum(table(a.list[[i]][a.list[[i]] %in% b.list[[i]]]))
#cat(tracemem(result), '\n')
}
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.193.135.208
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1446790865.A.50F.html
※ 编辑: CKPILOT (123.193.135.208), 11/06/2015 14:22:23
1F:→ celestialgod: 建议:append一个NULL的list并非好习惯... 11/06 14:25
2F:→ celestialgod: 还是先preallocate: result=vector('list', length( 11/06 14:25
3F:→ celestialgod: intersection)) 11/06 14:25
5F:→ CKPILOT: Thanks, it's really important for mem allocation 11/06 14:31
※ 编辑: CKPILOT (123.193.135.208), 11/06/2015 15:19:20
※ 编辑: CKPILOT (123.193.135.208), 11/06/2015 15:20:28