R-相对于匹配的名称,逐个元素地组合任意列表

时间:2018-07-10 11:10:35

标签: r

我有两个列表

m = list( 'a' = list( 'b' = list( 1, 2 ), 'c' = 3, 'b1' = 4, 'e' = 5 ) )

n = list( 'a' = list( 'b' = list( 10, 20 ), 'c' = 30, 'b1' = 40 ), 'f' = 50 )

m的结构为:

List of 1
 $ a:List of 4
  ..$ b :List of 2
  .. ..$ : num 1
  .. ..$ : num 2
  ..$ c : num 3
  ..$ b1: num 4
  ..$ e : num 5

n的结构是:

List of 2
 $ a:List of 3
  ..$ b :List of 2
  .. ..$ : num 10
  .. ..$ : num 20
  ..$ c : num 30
  ..$ b1: num 40
 $ f: num 50

想要这样的组合输出。同样,对于深度嵌套的列表,解决方案应该是通用的。

预期输出:

List of 2


$ a:List of 4
  ..$ b :List of 4
  .. ..$ : num 1
  .. ..$ : num 2
  .. ..$ : num 10
  .. ..$ : num 20
  ..$ c : num [1:2] 3 30
  ..$ b1: num [1:2] 4 40
  ..$ e : num 5
 $ f: num 50

已经看到了这一点,但是对于深度嵌套的列表来说还不够普遍

R: Combining Nested List Elements by Name

1 个答案:

答案 0 :(得分:2)

这是一种递归的方式:

public void inputAddress(String address) {
            INPUT_FIELD.sendKeys(address);
            //possibly here should be some sleep =(
            INPUT_FIELD.sendKeys(Keys.TAB);
    }

如果要合并多个嵌套列表(例如mergeList <- function(x, y){ if(is.list(x) && is.list(y) && !is.null(names(x)) && !is.null(names(y))){ ecom <- intersect(names(x), names(y)) enew <- setdiff(names(y), names(x)) res <- x if(length(enew) > 0){ res <- c(res, y[enew]) } if(length(ecom) > 0){ for(i in ecom){ res[i] <- list(mergeList(x[[i]], y[[i]])) } } return(res) }else{ return(c(x, y)) } } m = list( 'a' = list( 'b' = list( 1, 2 ), 'c' = 3, 'b1' = 4, 'e' = 5 ) ) n = list( 'a' = list( 'b' = list( 10, 20 ), 'c' = 30, 'b1' = 40 ), 'f' = 50 ) mn <- mergeList(m, n) str(mn) # List of 2 # $ a:List of 4 # ..$ b :List of 4 # .. ..$ : num 1 # .. ..$ : num 2 # .. ..$ : num 10 # .. ..$ : num 20 # ..$ c : num [1:2] 3 30 # ..$ b1: num [1:2] 4 40 # ..$ e : num 5 # $ f: num 50 m1m2),m3可能会有所帮助:

Reduce