Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.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
Iteration 在另一个数组中查找一个数组的所有值的索引,并从第三个数组中收集该索引中的值_Iteration - Fatal编程技术网

Iteration 在另一个数组中查找一个数组的所有值的索引,并从第三个数组中收集该索引中的值

Iteration 在另一个数组中查找一个数组的所有值的索引,并从第三个数组中收集该索引中的值,iteration,Iteration,我想在ParentIdle列表中找到子列表中匹配项的索引,然后将该索引中存在的值从Idle列表添加到子列表中,然后再次检查ParentIdle列表中所有匹配值的索引 我基本上是在尝试创建一个循环结构,它的结果如下所示:这似乎是可行的,但前提是您可以允许后代列表成为一个集合。如果不是,那么我不确定终止条件是什么,它只会一遍又一遍地添加相同索引的值。考虑到你在上面的评论中所说的,我认为一套是合适的。。。“我想循环执行此操作,直到没有更多的匹配项添加到后代列表” Set-downentlist=[2]

我想在ParentIdle列表中找到子列表中匹配项的索引,然后将该索引中存在的值从Idle列表添加到子列表中,然后再次检查ParentIdle列表中所有匹配值的索引


我基本上是在尝试创建一个循环结构,它的结果如下所示:

这似乎是可行的,但前提是您可以允许后代列表成为一个集合。如果不是,那么我不确定终止条件是什么,它只会一遍又一遍地添加相同索引的值。考虑到你在上面的评论中所说的,我认为一套是合适的。。。“我想循环执行此操作,直到没有更多的匹配项添加到后代列表”

Set-downentlist=[2]
def parentIdList=[0,1,2,3,2]
def idList=[1,2,3,4,5]
/** 
*首先:我想在
*家长主义者
*/
def findIndexMatches(设置子代列表、列表父代列表、列表子代列表){
列表索引=[]
def size=子代列表。size()
后代列表。每个{后代->
index.addAll(parentIdList.findIndexValues{it==subcentant})
}
将现有值从idList添加到DecendentList(后代列表、idList、索引)
//然后再次检查parentIdList,查找所有匹配值的索引。
如果(size!=degentList.size()){//没有向DecentList添加更多索引
findIndexMatches(子代列表、父代列表、子代列表)
}
}
/**
*然后从
*idList到后代列表
*/
def将现有值从idList添加到DecendentList(设置子代列表、列表idList、列表索引){
索引。每个{

后裔列表以下类似的东西似乎可以工作——虽然没有编写任何测试,因此可能会在不同的用例中失败——只是一个简单、惯用的递归解决方案

def descendentList = [2]
def parentIdList = [0,1,2,3,2]
def idList = [1,2,3,4,5]

def solve( List descendentList, List parentIdList, List idList ){ 
    List matchedIds = descendentList.inject( [] ){ result, desc ->
        result + idList[ parentIdList.findIndexValues{ it == desc } ] 
    }
    if ( matchedIds ){
        descendentList + solve( matchedIds, parentIdList, idList )
    } else {
        descendentList
    }
}

println solve( descendentList, parentIdList, idList )

也可以使用迭代器在不使用递归的情况下执行此操作:

class DescendantIterator<T> implements Iterator<T> {
    private final List<T> parents
    private List<T> output
    private final List<T> lookup
    private List<T> next

    DescendantIterator(List<T> output, List<T> parents, List<T> lookup) {
        this.output = output
        this.parents = parents
        this.lookup = lookup
    }

    boolean hasNext() { output }

    Integer next() {
        def ret = output.head()
        parents.findIndexValues { it == ret }.with { v ->
            if(v) { output += lookup[v] }
        }
        output = output.drop(1)
        ret
    }

    void remove() {}
}

def descendentList = [2]
def parentIdList = [0,1,2,3,2]
def idList = [1,2,3,4,5]

def values = new DescendantIterator<Integer>(descendentList, parentIdList, idList).collect()
类DescentAntiterator实现迭代器{
私人最终名单父母
私有列表输出
私有最终列表查找
下一个私人名单
子体反运算符(列表输出、列表父项、列表查找){
this.output=输出
这就是父母
this.lookup=查找
}
布尔hasNext(){output}
整数next(){
def ret=output.head()
parents.findIndexValues{it==ret}。带有{v->
如果(v){output+=lookup[v]}
}
输出=输出。下降(1)
ret
}
void remove(){}
}
def子代列表=[2]
def parentIdList=[0,1,2,3,2]
def idList=[1,2,3,4,5]
def values=新的子代反运算符(子代列表、父代列表、子代列表).collect()

在此之后,
values==[2,3,5,4]
首先构建一个从父id到其所有子id的映射。接下来查找输入的结果,并迭代新找到的结果,只要没有更多结果

def parentIdList = [0,1,2,3,2]
def idList = [1,2,3,4,5]

tree = [parentIdList, idList].transpose().groupBy{it[0]}.collectEntries{ [it.key, it.value*.get(1)] }

def childs(l) {
    l.collect{ tree.get(it) }.findAll().flatten().toSet()
}

def descendants(descendentList) {
    def newresults = childs(descendentList)
    def results = [].toSet() + descendentList
    while (newresults.size()) {
        results.addAll(newresults)
        newresults = childs(newresults) - results
    }
    return results
}

assert descendants([2]) == [2,3,4,5].toSet()
assert descendants([2,1]) == [1,2,3,4,5].toSet()
assert descendants([3]) == [3,4].toSet()

你能更好地解释一下你在问什么吗?也许是一个运行测试的例子?给定这段代码,如果你向DegentList添加一个值,使DegentList=[2,1],那么输出是[2,1,3,5,2,4,3,5,4]。我认为这与允许将同一索引多次添加到子列表中是同一问题。根据其他响应,事实上,并没有声明该数字只能添加一次。也可以通过添加unique()调用来解决(或者在合并时进行检查。我在这里有与@rhinds solution的评论中所述相同的问题。我想你可以简单地调用它的.unique()。这取决于他们希望解决方案是什么;-)他们没有尽我所能在问题中指定see@dspano还可以使用
def lookups=parentIdList.indexed().groupBy{it.value}.collectEntries{k,v->[k,idList[v.keySet()]]}
,但我目前找不到一个干净的地方来使用它;-)
def parentIdList = [0,1,2,3,2]
def idList = [1,2,3,4,5]

tree = [parentIdList, idList].transpose().groupBy{it[0]}.collectEntries{ [it.key, it.value*.get(1)] }

def childs(l) {
    l.collect{ tree.get(it) }.findAll().flatten().toSet()
}

def descendants(descendentList) {
    def newresults = childs(descendentList)
    def results = [].toSet() + descendentList
    while (newresults.size()) {
        results.addAll(newresults)
        newresults = childs(newresults) - results
    }
    return results
}

assert descendants([2]) == [2,3,4,5].toSet()
assert descendants([2,1]) == [1,2,3,4,5].toSet()
assert descendants([3]) == [3,4].toSet()