[英]create list of list with different length
我正在嘗試檢查某些單詞是否是名詞,動詞等。
因此,我的最終輸出應該是單詞及其分類的列表。
考慮以下腳本:
library(data.table)
library(xml2)
random_words_2 <- c("aa","ab","ac")
dic <- list()
dics <- list()
for (i in 1:3){
h <- paste0("http://www.oxforddictionaries.com/definition/english/",random_words_2[i])
html <- read_html(h)
oxford <- html_nodes(html, css = ".partOfSpeech")
n <- length(oxford)
for (m in 1:n) {
word <- as_list(oxford[[m]])
w <- unlist(word[1])
dic[[m]] <- data.table(as.character(w))
}
dics <- rbindlist(dics, dic,use.names = TRUE,fill=FALSE)
}
有些單詞具有多個分類,例如動詞,副詞等。因此,列表的大小會有所不同。 我嘗試了上面的代碼,但是dics變量應該為我提供解決方案:
空的data.table(0行和0 cols)
但是,dic變量給出:
[[1]] V1 1:名詞
[[2]] V1 1:縮寫
有人可以解釋為什么會這樣嗎,還有沒有更有效的方法來解決呢?
謝謝
將for循環替換為此:
dics <- list()
for (i in 1:3){
h <- paste0("http://www.oxforddictionaries.com/definition/english/",random_words_2[i])
html <- read_html(h)
oxford <- html_nodes(html, css = ".partOfSpeech")
n <- length(oxford)
dic <- list()
for (m in 1:n)
{
word <- as_list(oxford[[m]])
w <- unlist(word[1])
dic[[m]] <- data.table(as.character(w))
}
dics <- c(dics, setNames(list(dic),random_words_2[i]))
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.