2016-06-13 31 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