R - 使用覆盖和递归合并列表

zee*_*eez 8 recursion r list

假设我有两个名字列表,

a = list( a=1, b=2, c=list( d=1, e=2 ), d=list( a=1, b=2 ) )
b = list( a=2, c=list( e=1, f=2 ), d=3, e=2 )
Run Code Online (Sandbox Code Playgroud)

我想以递归方式合并这些列表,如果第二个参数包含冲突值,则覆盖条目.即预期的产量将是

$a
[1] 2

$b
[1] 2

$c
$c$d
[1] 1

$c$e
[1] 1

$c$f
[1] 2

$d
[1] 3

$e
[1] 2
Run Code Online (Sandbox Code Playgroud)

任何提示?

mat*_*fee 9

我想你必须在这里编写自己的递归函数.

一个接收两个列表的函数,list1list2.如果:

  • list1[[name]]存在但不存在list2[[name]],使用list1[[name]];
  • list1[[name]]存在以及list2[[name]]两者都不是列表,使用list2[[name]];
  • 否则,使用list1[[name]]list2[[name]]作为新列表递归.

就像是:

myMerge <- function (list1, list2) {
    allNames <- unique(c(names(list1), names(list2)))
    merged <- list1 # we will copy over/replace values from list2 as necessary
    for (x in allNames) {
        # convenience
        a <- list1[[x]]
        b <- list2[[x]]
        if (is.null(a)) {
            # only exists in list2, copy over
            merged[[x]] <- b
        } else if (is.list(a) && is.list(b)) {
            # recurse
            merged[[x]] <- myMerge(a, b)
        } else if (!is.null(b)) {
            # replace the list1 value with the list2 value (if it exists)
            merged[[x]] <- b
        }
    }
    return(merged)
}
Run Code Online (Sandbox Code Playgroud)

注意事项 - 如果要合并的列表很奇怪,您可能会得到奇怪的输出.例如:

a <- list( a=list(a=1, b=2), b=3 )
b <- list( a=2 )
Run Code Online (Sandbox Code Playgroud)

然后你的合并列表了a=2, b=3.这是因为来自b$a覆盖值的值a$a,即使a$a是一个列表(您没有指定如果是这种情况会发生什么).但是,修改myMerge以处理这些类型的案例非常简单.请记住 - 用于is.list测试它是否是列表,并is.null(myList$a)查看a列表中是否存在条目myList.


这是使用以下内容的"矢量化"版本sapply:

merge.lists <- function(a, b) {
    a.names <- names(a)
    b.names <- names(b)
    m.names <- sort(unique(c(a.names, b.names)))
    sapply(m.names, function(i) {
        if (is.list(a[[i]]) & is.list(b[[i]])) merge.lists(a[[i]], b[[i]])
        else if (i %in% b.names) b[[i]]
        else a[[i]]
    }, simplify = FALSE)
}
Run Code Online (Sandbox Code Playgroud)


小智 9

我不太确定这里是否需要自定义功能.有一个功能utils::modifyList()来执行这个完全相同的操作!有关详细信息,请参阅modifyList.

a <- list( a=1, b=2, c=list( d=1, e=2 ), d=list( a=1, b=2 ) )
b <- list( a=2, c=list( e=1, f=2 ), d=3, e=2 )

modifyList(a, b) # updates(modifies) 'a' with 'b'
Run Code Online (Sandbox Code Playgroud)

这给出了以下内容

$a
[1] 2

$b
[1] 2

$c
$c$d
[1] 1

$c$e
[1] 1

$c$f
[1] 2

$d
[1] 3

$e
[1] 2 
Run Code Online (Sandbox Code Playgroud)