作者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/m.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