r中嵌套列表的第二个[给定]级别的提取名称

发布于 2025-02-13 08:25:11 字数 709 浏览 1 评论 0原文

我有一个嵌套列表。该列表的每个级别均为命名(如提供的虚拟示例中)。我想从初始列表的第二级中提取名称(唯一),因此我将能够在进一步的操作中使用它们。

我知道如何分两个步骤进行操作,但是我想知道是否有更高效/优雅的解决方案。我也想知道,正则方法是否会更快(我在正则是菜鸟)。

这是一个虚拟列表:

x <- list(one = list(one_1 = list(seq = 1:9, start = 1, end = 5),
                     one_2 = list(seq = 2:11, start = 2, end = 6),
                     one_3 = list(seq = 3:12, start = 3, end = 7)),
          two = list(two_1 = list(seq = 1:13, start = 8, end = 222),
                     two_2 = list(seq = 1:14, start = 13, end = 54)))

这是我的代码:

allnames <- names(rapply(x, function(x) head(x, 1)))
desirednames <- unique(sapply(strsplit(allnames, ".", fixed=TRUE), "[", 2))

I have a nested list. Each level of this list is named (as in provided dummy example). I want to extract the names (unique) from the 2nd level of my initial list, so I will be able to use them in some further operations.

I know how to do it in two steps, but I wonder whether there is more efficient/elegant solution. Also I wonder whether the regex approach would be faster (I am noob at regex).

Here is a dummy list:

x <- list(one = list(one_1 = list(seq = 1:9, start = 1, end = 5),
                     one_2 = list(seq = 2:11, start = 2, end = 6),
                     one_3 = list(seq = 3:12, start = 3, end = 7)),
          two = list(two_1 = list(seq = 1:13, start = 8, end = 222),
                     two_2 = list(seq = 1:14, start = 13, end = 54)))

And here is my code:

allnames <- names(rapply(x, function(x) head(x, 1)))
desirednames <- unique(sapply(strsplit(allnames, ".", fixed=TRUE), "[", 2))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

童话里做英雄 2025-02-20 08:25:11

基本 r的第二级解决方案:R:

unlist(lapply(names(x), function(n) names(x[[n]])))
[1] "one_1" "one_2" "one_3" "two_1" "two_2"

Solution for second level in base R:

unlist(lapply(names(x), function(n) names(x[[n]])))
[1] "one_1" "one_2" "one_3" "two_1" "two_2"
素罗衫 2025-02-20 08:25:11

基于purrr :: map_depth的可能解决方案:

library(tidyverse)

map_depth(x, 1, names) %>% unlist(use.names = F)

#> [1] "one_1" "one_2" "one_3" "two_1" "two_2"

A possible solution, based on purrr::map_depth:

library(tidyverse)

map_depth(x, 1, names) %>% unlist(use.names = F)

#> [1] "one_1" "one_2" "one_3" "two_1" "two_2"
花心好男孩 2025-02-20 08:25:11

这样做的一种简洁的方法:

unlist(lapply(x, \(x) attributes(x)[['names']])) 
#    one1    one2    one3    two1    two2 
# "one_1" "one_2" "one_3" "two_1" "two_2"

如果列表元素只有“ names”属性,则简化为:

unlist(lapply(x, attributes))
# one.names1 one.names2 one.names3 two.names1 two.names2 
#    "one_1"    "one_2"    "one_3"    "two_1"    "two_2" 

如果名称烦恼您,请将其输入unname

unlist(lapply(x, attributes)) |> unname()
# [1] "one_1" "one_2" "one_3" "two_1" "two_2"

A concise way of doing this:

unlist(lapply(x, \(x) attributes(x)[['names']])) 
#    one1    one2    one3    two1    two2 
# "one_1" "one_2" "one_3" "two_1" "two_2"

If the list elements just have the "names" attribute, this simplifies to:

unlist(lapply(x, attributes))
# one.names1 one.names2 one.names3 two.names1 two.names2 
#    "one_1"    "one_2"    "one_3"    "two_1"    "two_2" 

If the names annoy you, pipe it into unname.

unlist(lapply(x, attributes)) |> unname()
# [1] "one_1" "one_2" "one_3" "two_1" "two_2"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文