Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 所有列表元素的索引路径_R - Fatal编程技术网

R 所有列表元素的索引路径

R 所有列表元素的索引路径,r,R,如何获取所有列表元素的索引路径? tree <- list(1:6,b=list(a=20:23,b=28),c=c("a","b")) tree您的基本深度优先搜索。Map/unlist组合用于保持结果列表平坦,因此else子句的叶级结果必须包装在列表中。idx将当前索引保存到结构中,acc是树叶的累积索引 index.leaves <- function(root) { walk <- function(node,idx,acc) { if(length(no

如何获取所有列表元素的索引路径?

tree <- list(1:6,b=list(a=20:23,b=28),c=c("a","b"))

tree您的基本深度优先搜索。Map/unlist组合用于保持结果列表平坦,因此else子句的叶级结果必须包装在列表中。
idx
将当前索引保存到结构中,
acc
是树叶的累积索引

index.leaves <- function(root) {
  walk <- function(node,idx,acc) {
    if(length(node)>1) {
      r<-Map(function(child,i) walk(child,c(idx,i),acc),node,seq_along(node))
      unlist(r,recursive=FALSE)
    }
    else {
      list(c(acc,idx))
    }
  }
  walk(root,NULL,c())
}
13人名单 $:num[1:2]1 $:num[1:2]12 $:num[1:2]13 $:num[1:2]14 $:num[1:2]15 $:num[1:2]16 $b.a1:num[1:3]21 $b.a2:num[1:3]2 $b.a3:num[1:3]2113 $b.a4:num[1:3]2114 $b.b:num[1:2]2 $c.a:num[1:2]3 1 $c.b:num[1:2]32
有两种递归方法,它们使用不同的方式展平结果列表:

方法1。深度优先搜索;到达底部后,将结果保存到上层变量
L

nametree <- function(X) {
  L <- NULL

  rec <- function(X, prefix = NULL) {
    if( length(X) == 1 ) {L <<- c(L,list(prefix)); return()}    
    sapply(seq_along(X), function(i) rec(X[[i]], c(prefix,i)))
  }

  rec(X)
  L
}
两种方法都返回相同的结果,可用于索引原始
列表:

> tree[[z[[7]]]]
[1] 20
> tree[[z2[[7]]]]
[1] 20

你的问题太简短了。我不明白。@Roland我想补充更多信息,但我不确定要补充什么细节。你能告诉我你不懂什么吗?@Roland我补充了一点-你想要的是散列注释
width-first search
depth-first search
图形探索算法。使用
length(1)=1
length(“a”)
来检测退出递归条件的叶子。@Math,谢谢你指导我找到理论来源,这也会有帮助。谢谢你的快速回复。决定接受哪一个并不容易。你和塔利波夫的答案都很有趣。再次感谢。我认为如果(长度(节点)>1)测试不是非常健壮,并且不适合所有情况,最好使用if(typeof(节点)=“list”|长度(节点)>1)。我已经在更大的真实案例树上测试了这个函数,而且更长的测试更适合。谢谢你的快速回复。 List of 13 $ : num [1:2] 1 1 $ : num [1:2] 1 2 $ : num [1:2] 1 3 $ : num [1:2] 1 4 $ : num [1:2] 1 5 $ : num [1:2] 1 6 $ b.a1: num [1:3] 2 1 1 $ b.a2: num [1:3] 2 1 2 $ b.a3: num [1:3] 2 1 3 $ b.a4: num [1:3] 2 1 4 $ b.b : num [1:2] 2 2 $ c.a : num [1:2] 3 1 $ c.b : num [1:2] 3 2
nametree <- function(X) {
  L <- NULL

  rec <- function(X, prefix = NULL) {
    if( length(X) == 1 ) {L <<- c(L,list(prefix)); return()}    
    sapply(seq_along(X), function(i) rec(X[[i]], c(prefix,i)))
  }

  rec(X)
  L
}
nametree2 <- function(X) {

  rec <- function(X, prefix = NULL) {
    if( length(X) == 1 ) return(prefix)    
    lapply(seq_along(X), function(i) rec(X[[i]], c(prefix,i)))
  }

  z <- rec(X)

  # Convert nested list into a simple list  
  z2 <- rapply(z,length)
  split(unlist(z),rep(seq_along(z2),z2))
}

z <- nametree(tree)
z2 <- nametree2(tree)
> tree[[z[[7]]]]
[1] 20
> tree[[z2[[7]]]]
[1] 20