list 如何展平列表列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16300344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 02:00:23  来源:igfitidea点击:

How to flatten a list of lists?

rlisttm

提问by dnagirl

The tmpackage extends cso that, if given a set of PlainTextDocuments it automatically creates a Corpus. Unfortunately, it appears that each PlainTextDocumentmust be specified separately.

tm包进行扩展,c因此,如果给定一组PlainTextDocuments,它会自动创建一个Corpus. 不幸的是,似乎每个都PlainTextDocument必须单独指定。

e.g. if I had:

例如,如果我有:

foolist <- list(a, b, c); # where a,b,c are PlainTextDocument objects

I'd do this to get a Corpus:

我这样做是为了得到一个Corpus

foocorpus <- c(foolist[[1]], foolist[[2]], foolist[[3]]);

I have a list of lists of 'PlainTextDocuments that looks like this:

我有一个'PlainTextDocument看起来像这样的s列表:

> str(sectioned)
List of 154
 $ :List of 6
  ..$ :Classes 'PlainTextDocument', 'TextDocument', 'character'  atomic [1:1] Developing assessment models   Developing models
  .. .. ..- attr(*, "Author")= chr "John Smith"
  .. .. ..- attr(*, "DateTimeStamp")= POSIXlt[1:1], format: "2013-04-30 12:03:49"
  .. .. ..- attr(*, "Description")= chr(0) 
  .. .. ..- attr(*, "Heading")= chr "Research Focus"
  .. .. ..- attr(*, "ID")= chr(0) 
  .. .. ..- attr(*, "Language")= chr(0) 
  .. .. ..- attr(*, "LocalMetaData")=List of 4
  .. .. .. ..$ foo           : chr "bar"
  .. .. .. ..$ classification: chr "Technician"
  .. .. .. ..$ team          : chr ""
  .. .. .. ..$ supervisor    : chr "Bill Jones"
  .. .. ..- attr(*, "Origin")= chr "Smith-John_e.txt"

#etc., all sublists have 6 elements

So, to get all my PlainTextDocuments into a Corpus, this would work:

因此,要将我所有的PlainTextDocuments 放入 a Corpus,这将起作用:

sectioned.Corpus <- c(sectioned[[1]][[1]], sectioned[[1]][[2]], ..., sectioned[[154]][[6]])

Can anyone suggest an easier way, please?

任何人都可以建议更简单的方法吗?

ETA: foo<-unlist(foolist, recursive=FALSE)produces a flat list of PlainTextDocuments, which still leaves me with the problem of feeding a list element by element to c

ETA:foo<-unlist(foolist, recursive=FALSE)生成一个纯文本文档的平面列表,这仍然给我留下了一个逐个元素提供列表的问题c

采纳答案by DrDom

I expect that unlist(foolist)will help you. It has an option recursivewhich is TRUEby default.

我希望这unlist(foolist)会帮助你。它有一个选项recursiveTRUE默认。

So unlist(foolist, recursive = FALSE)will return the list of the documents, and then you can combine them by:

Sounlist(foolist, recursive = FALSE)将返回文档列表,然后您可以通过以下方式组合它们:

do.call(c, unlist(foolist, recursive=FALSE))

do.calljust applies the function cto the elements of the obtained list

do.call只是将函数应用于c获得的列表的元素

回答by Michael

Here's a more general solution for when lists are nested multiple times and the amount of nesting differs between elements of the lists:

当列表多次嵌套并且列表元素之间的嵌套量不同时,这是一个更通用的解决方案:

 flattenlist <- function(x){  
  morelists <- sapply(x, function(xprime) class(xprime)[1]=="list")
  out <- c(x[!morelists], unlist(x[morelists], recursive=FALSE))
  if(sum(morelists)){ 
    Recall(out)
  }else{
    return(out)
  }
}