2016-06-13 81 views
0

我想从给定的条件字典中删除单词。 我想这样做,在下一次迭代时,字典将计算新的一个,最后一项删除,所以它不会再计算。双回路去除物品

// sample data 
    $dict = ['aaa', 'aaan','aba', 'abat', 'ime', 'iso', 'nime', 'tiso',]; 
    $unique = ['abatiso', 'aaanime']; 

    // could use while to further optimize unset (and remove on the fly) http://php.net/manual/en/control-structures.foreach.php#88578 
    while (list($key_word, $word) = each($unique)) { // $key is unused, just for the optimization that the whille provides 
     foreach ($dict as $key_other => $other) { 

// ... conditions calculations 

     unset($unique[$key_word]); 
} 
} 
    echo "n compounds: " . count($compounds) . NL; 

如果我设置内部循环而不是foreach作为外部,我得到0结果,它立即终止。

现在,我得到重复的结果,如:

   // Removed: abatiso => wc: aba + tiso = abatiso 
       // Removed: abatiso => wc: abat + iso = abatiso 
       // Removed: abatiso => wc: abati + so = abatiso 
       // Removed: abatiso => wc: abatis + o = abatiso 

我怎样才能使它所以它删除的话,不会对下一个迭代再次proccess呢?

一些测试数据:

Removed: aaaaaah => wc: aaaa + aah = aaaaaah 
Removed: aaaaaah => wc: aaaaaa + h = aaaaaah 
Removed: aaaaargh => wc: aaa + aargh = aaaaargh 
Removed: aaaalead => wc: aaaa + lead = aaaalead 
Removed: aaabbbccc => wc: aaab + bbccc = aaabbbccc 
Removed: aaacomix => wc: aaa + comix = aaacomix 
Removed: aaagak => wc: aaa + gak = aaagak 
Removed: aaahh => wc: aaa + hh = aaahh 
Removed: aaainc => wc: aaa + inc = aaainc 
Removed: aaainc => wc: aaai + nc = aaainc 
Removed: aaanet => wc: aaa + net = aaanet 
Removed: aaanet => wc: aaan + et = aaanet 
Removed: aaanime => wc: aaa + nime = aaanime 
Removed: aaanime => wc: aaan + ime = aaanime 
Removed: aaaron => wc: aaa + ron = aaaron 
Removed: aabbcc => wc: aab + bcc = aabbcc 
Removed: aabmup => wc: aab + mup = aabmup 
Removed: aabre => wc: aab + re = aabre 
Removed: aabybro => wc: aaby + bro = aabybro 
Removed: aacap => wc: aac + ap = aacap 
Removed: aacap => wc: aaca + p = aacap 
Removed: aaccording => wc: aac + cording = aaccording 
Removed: aacd => wc: aac + d = aacd 
Removed: aachener => wc: aach + ener = aachener 
Removed: aachener => wc: aachen + er = aachener 
Removed: aacisuan => wc: aaci + suan = aacisuan 
Removed: aacisuan => wc: aacis + uan = aacisuan 
Removed: aacult => wc: aac + ult = aacult 
我不使用内环内休息,因为我必须做的计算也

+0

请在您的问题和代码更加具体。 – Eiko

+0

@Eiko你不明白? – Cristo

+0

我正在考虑类似于''for($ i = 0; $ i Cristo

回答

0

代码有错误。您在两个地方设置了两个不同含义的$key值。起初,您将其分配在list(..声明中,然后再次在foreach循环中作为$dict中值的键值保留。

作为一条经验法则,当您遍历该列表时,从列表中取消设置元素永远不会好。您最好将您在列表中处理的项目保存在列表中,而不要再次处理它们。如果你想要你可以稍后删除这些项目,你完成了循环过unique

如果我明白你的问题正确,这将是很长的路要走:

$toUnset = []; 
foreach ($unique as $key => $word) { 
    if (!in_array($word, $toUnset)) { 
     foreach ($dict as $other) { 

      //do your processing 
      $toUnset[] = $word; 
     } 
    } 
} 
+0

对不起,我没有使用$ key,代码有点凌乱,对这么多正在进行的更改和尝试,我会更正。 我宁愿在飞行中这么做,因为它运行得更快,因为目前需要运行将近24小时:D – Cristo

+0

您的字典有多少项? 24小时听起来像是一个严重的运行问题。 – cb0

+0

只有300k和110k。我认为循环内部的一些strlen和strpos以及一个自定义的binary_search(而不是内置的php in_array/search,因为字典被排序)会让它太慢。 我正在尝试isset vs strlen hack(http://stackoverflow.com/questions/6955913/isset-vs-strlen-a-fast-clear-string-length-calculation),看起来,promissing – Cristo