Re: [問題] 比對兩個list

看板R_Language作者 (新年快樂!)時間10年前 (2015/11/06 14:21), 10年前編輯推噓0(005)
留言5則, 2人參與, 最新討論串3/3 (看更多)
#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://www.ptt.cc/bbs/R_Language/M.1446790865.A.50F.html ※ 編輯: CKPILOT (123.193.135.208), 11/06/2015 14:22:23

11/06 14:25, , 1F
建議:append一個NULL的list並非好習慣...
11/06 14:25, 1F

11/06 14:25, , 2F
還是先preallocate: result=vector('list', length(
11/06 14:25, 2F

11/06 14:25, , 3F
intersection))
11/06 14:25, 3F

11/06 14:27, , 4F
11/06 14:27, 4F

11/06 14:31, , 5F
Thanks, it's really important for mem allocation
11/06 14:31, 5F
※ 編輯: CKPILOT (123.193.135.208), 11/06/2015 15:19:20 ※ 編輯: CKPILOT (123.193.135.208), 11/06/2015 15:20:28
文章代碼(AID): #1MF4RHKF (R_Language)
文章代碼(AID): #1MF4RHKF (R_Language)