douniewei6346 2016-06-13 08:32
浏览 46

双循环移除项目

I want to remove words from a dictionary given certain conditions. I would like to do so that in the next iteration the dictionary will calculate over the new one, that has that last item removed, so it won't calculate again.

// 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;

If I set the inner loop with a while instead foreach as the outer, I get 0 results, it terminates inmediately.

For now, I'm getting duplicate results like:

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

How can I make it so it removes the word and won't proccess it again on next iteration?

Some test data:

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

I'm not using a break inside the inner loop because I have to do calculations also.

  • 写回答

1条回答 默认 最新

  • douzhuo3233 2016-06-13 09:01
    关注

    There is an error in your code. You set the $key value in two places with two different meanings. At first you assign it in the list(.. statement and then again in the foreach loop as key value holder for the values in $dict.

    As a rule of thumb it is never good to unset elements from a list while you are iterating over that list. You better save the items you processed in a list and don't process them again. If you want to you can remove those items later on, after you finished the loop over unique.

    If I understand your question correctly this would be a way to go:

    $toUnset = [];
    foreach ($unique as $key => $word) {
        if (!in_array($word, $toUnset)) {
            foreach ($dict as $other) {
    
                //do your processing
                $toUnset[] = $word;
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度