- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个列表
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
看过这个,但是对深度嵌套的列表不够通用
最佳答案
这是一个递归的方法:
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
如果您有多个嵌套列表(例如,m1
、m2
和 m3
)要合并,Reduce
可能会有帮助:
Reduce(mergeList, list(m1, m2, m3))
关于R - 根据匹配的名称逐个元素组合任意列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51263678/
我想要以下内容: void foo( /* something representing a function f */, /* arguments a1, a2, etc. in s
简而言之,我想声明一个这样的特征: trait Test { def test(amount: Int): A[Int] // where A must be a Monad } 这样我就可以
在 GWT 中,如何在 onModuleLoad 方法中插入框架集以及相对嵌套的框架集和框架,以合并许多小程序和其他小部件和 HTML?代码片段是: 公共(public)类 MainEntryPoin
这个问题在这里已经有了答案: How do I best simulate an arbitrary univariate random variate using its probability
我对java相当陌生,并且习惯于枚举本质上只不过是一个命名的整数列表。 现在我正在编写一个实现,其中父类有几个采用枚举值作为参数的方法。枚举将在子类中定义,并且会略有不同。由于枚举基本上看起来像类,所
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
想象一下 6-7 台服务器的设置都完全相同Java 版本“1.6.0_18”OpenJDK 运行时环境 (IcedTea6 1.8) (fedora-36.b18.fc11-i386)OpenJDK
这个问题在这里已经有了答案: What are some uses of template template parameters? (10 个答案) 关闭 4 年前。 我有一个根据策略舍入值的函数
我正在寻找如何在 Java 中给定一个 Async CompletableFutures 列表,以便前 N 个中的任何一个成功完成或失败。除非没有 N 次成功,否则忽略任何失败。 有这方面的例子吗?
我面临的问题是项目已经使用集群编程来分配任务。 if (cluster.isMaster) { // Fork workers. for (var i = 0; i { }); } el
我正在为 Luxology modo(3D 和 VFX 应用程序)编写脚本,该脚本使用 python 作为脚本语言。在我的脚本中的某个位置,我正在读取从其他应用程序输出的文本文件,并从该文本文件的行创
这个问题在这里已经有了答案: Fast arbitrary distribution random sampling (inverse transform sampling) (5 个答案) 关闭
我只是遇到了一个问题,我有一个结构数组,例如 package main import "log" type Planet struct { Name string `json:"
我正在尝试将 class ResponseResult 编码为 json case class ResponseResult (var Code : Int, var
我想将一个矩阵中的一个 block 复制到另一个矩阵的一部分中。要将其与任何类型的 n 维数组一起使用,我需要通过 [] 运算符应用带有偏移量的列表。有办法做到这一点吗? mat_bigger[0:5
我有一个匹配一组数字和字母的正则表达式。但是我希望能够排除任何三个连续的字母。这是为了防止意外形成单词或缩写。 我的表达如下。它还排除了一些类似的字符,如 0、o、O 和 1、i、I、l): ^[2-
根据documentation . 应匹配任何字符,但不匹配重音字符。 mysql> select 'test' regexp 't.st'; +----------------------+ | '
我该如何用 JavaScript 编写这个 if 语句? if(url == "http://www.google.com/" && "*") { ... } * 需要灵活并接受添加到第一个变量上
我知道 cPython 有一个 GIL,因此如果不使用多处理模块,您的脚本就无法在多个内核上运行。但是有什么可以阻止内置功能,例如使用多核进行排序吗?我不了解 cPython 结构,但我想我要问的问题
寻找命令行 gdb 的替代方法来检查 OSX 上的核心转储 - 有没有办法让 Xcode 打开带有调试符号的任意核心转储? 最佳答案 您是否尝试过使用 MachOView 1? 听起来它可能适用于查看
我是一名优秀的程序员,十分优秀!