## 如何还原列表结构？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (2)
• 关注 (0)
• 查看 (49)

``````z <- list(z1 = list(a = 1, b = 2, c = 3), z2 = list(b = 4, a = 1, c = 0))
``````

``````o <- list(a = list(z1 = 1, z2 = 1), b = list(z1 = 2, z2 = 4), c = list(z1 = 3, z2 = 0))
``````

``````revert_list_str_1 <- function(ls) {
res <- lapply(names(ls[[1]]), function(n, env) {
name <- paste(n, 'elements', sep = '_')
assign(name, vector('list', 0))
inner <- sapply(ls, function(x) {
assign(name, c(get(name), x[which(names(x) == n)]))
})
names(inner) <- names(ls)

inner
})
names(res) <- names(ls[[1]])

res
}
``````

``````List of 3
\$ a:List of 2
..\$ z1: num 1
..\$ z2: num 1
\$ b:List of 2
..\$ z1: num 2
..\$ z2: num 4
\$ c:List of 2
..\$ z1: num 3
..\$ z2: num 0
``````

``````# yes 1.1
# yes 1.2
# yes 2.1, not 2.2, not 2.3
revert_list_str_1 <- function(ls) { # @leodido
# see above
}

# not 1.1
# not 1.2
# not 2.1, not 2.2, not 2.3
revert_list_str_2 <- function(ls) { # @mnel
# convert each component of list to a data.frame
# so rbind.data.frame so named elements are matched
x <- data.frame((do.call(rbind, lapply(ls, data.frame))))
# convert each column into an appropriately named list
o <- lapply(as.list(x), function(i, nam) as.list(`names<-`(i, nam)), nam = rownames(x))

o
}

# yes 1.1
# yes 1.2
# yes 2.1, not 2.2, yes 2.3
revert_list_str_3 <- function(ls) { # @mnel
# unique names
nn <- Reduce(unique, lapply(ls, names))
# convert from matrix to list `[` used to ensure correct ordering
as.list(data.frame(do.call(rbind,lapply(ls, `[`, nn))))
}

# yes 1.1
# yes 1.2
# yes 2.1, not 2.2, yes 2.3
revert_list_str_4 <- function(ls) { # @Josh O'Brien
# get sub-elements in same order
x <- lapply(ls, `[`, names(ls[[1]]))
# stack and reslice
apply(do.call(rbind, x), 2, as.list)
}

# not 1.1
# not 1.2
# not 2.1, not 2.2, not 2.3
revert_list_str_5 <- function(ls) { # @mnel
apply(data.frame((do.call(rbind, lapply(ls, data.frame)))), 2, as.list)
}

# not 1.1
# not 1.2
# not 2.1, yes 2.2, yes 2.3
revert_list_str_6 <- function(ls) { # @baptiste + @Josh O'Brien
b <- recast(z, L2 ~ L1)
apply(b, 1, as.list)
}

# yes 1.1
# yes 1.2
# not 2.1, yes 2.2, yes 2.3
revert_list_str_7 <-  function(ll) { # @Josh O'Brien
nms <- unique(unlist(lapply(ll, function(X) names(X))))
ll <- lapply(ll, function(X) setNames(X[nms], nms))
ll <- apply(do.call(rbind, ll), 2, as.list)
lapply(ll, function(X) X[!sapply(X, is.null)])
}
``````

`list(z1 = list(a = 1, b = 2, c = 3), z2 = list(a = 0, b = 3, d = 22, f = 9))`.

``````Unit: microseconds
expr       min         lq     median         uq       max
1 func_1   250.069   467.5645   503.6420   527.5615  2028.780
2 func_3   204.386   393.7340   414.5485   429.6010  3517.438
3 func_4    89.922   173.7030   189.0545   194.8590  1669.178
4 func_6 11295.463 20985.7525 21433.8680 21934.5105 72476.316
5 func_7   348.585   387.0265   656.7270   691.2060  2393.988
``````

`list(z1 = list(a = 1, b = 2, c = 'ciao'), z2 = list(a = 0, b = 3, c = 5))`.

`revert_list_str_6`

``````Unit: microseconds
expr     min       lq   median       uq      max
1 func_1 249.558 483.2120 502.0915 550.7215 2096.978
2 func_3 210.899 387.6835 400.7055 447.3785 1980.912
3 func_4  92.420 170.9970 182.0335 192.8645 1857.582
4 func_7 257.772 469.9280 477.8795 487.3705 2035.101
``````

`revert_list_str_4`

`list(z1 = list(a = 1, b = m, c = 'ciao'), z2 = list(a = 0, b = 3, c = m))`.

`m`是一个3x3的整数矩阵，并`revert_list_str_6`已被排除在外。

``````Unit: microseconds
expr     min       lq   median       uq      max
1 func_1 261.173 484.6345 503.4085 551.6600 2300.750
2 func_3 209.322 393.7235 406.6895 449.7870 2118.252
3 func_4  91.556 174.2685 184.5595 196.2155 1602.983
4 func_7 252.883 474.1735 482.0985 491.9485 2058.306
``````

```fun <-  function(ll) {
nms <- unique(unlist(lapply(ll, function(X) names(X))))
ll <- lapply(ll, function(X) setNames(X[nms], nms))
ll <- apply(do.call(rbind, ll), 2, as.list)
lapply(ll, function(X) X[!sapply(X, is.null)])
}

## An example of an 'unbalanced' list
z <- list(z1 = list(a = 1, b = 2),
z2 = list(b = 4, a = 1, c = 0))
## Try it out
fun(z)```

```z <- list(z1 = list(a = 1, b = 2, c = 3), z2 = list(b = 4, a = 1, c = 0))

zz <- lapply(z, `[`, names(z[[1]]))   ## Get sub-elements in same order
apply(do.call(rbind, zz), 2, as.list) ## Stack and reslice```

### 腾讯云 COS 怎么才能外链调用 m3u8 到别的网站播放？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### Ubuntu搭建的WordPress如何修改php.ini？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

php新手很多不知道怎么查配置文件在哪，这里提供一个很简单的方法 使用 php -i 命令可以打印php的详细信息，可以把这堆东西输出一下 php -i > outputphp.txt，结合 grep 查找命令 php -i| grep php.ini 打印结果如下 Config...... 展开详请

### 归档存储采用的存储介质是什么, 安全可靠吗？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### redis数据库应该怎样连接？？？

Swoole · PHP开发工程师 (已认证)

As a PHP Developer

### 如何使用holer实现从外网访问本地WEB应用？

Dingda

Dingda · 站长 (已认证)