标签: nested-loops

Rails中令人讨厌的深嵌套循环

我有这个嵌套的循环,深入4级,找到所有图像小部件并计算它们的大小.这看起来真的很低效和讨厌!我曾想过将organization_id放在widget模型中,所以我可以调用像organization.widgets这样的东西.(named_scope),但我觉得这是一个糟糕的捷径.有没有更好的办法?谢谢

class Organization < ActiveRecord::Base
...
  def get_image_widget_total
    total_size = 0
    self.trips.each do |t|
      t.phases.each do |phase|
        phase.pages.each do |page|
          page.widgets.each do |widget|
            if widget.widget_type == Widget::IMAGE
             total_size += widget.image_file_size
            end
         end
       end
     end
    end
    return total_size
  end
...
end
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-loops

1
推荐指数
1
解决办法
853
查看次数

帮助For循环.值重复

$teams = array(1, 2, 3, 4, 5, 6, 7, 8);
$game1 = array(2, 4, 6, 8);
$game2 = array();
Run Code Online (Sandbox Code Playgroud)

如果teams[x]不在game1那么插入game2

for($i = 0; $i < count($teams); $i++){
    for($j = 0; $j < count($game1); $j++){
        if($teams[$i] == $game1[$j]){
            break;
        } else {
            array_push($game2, $teams[$i]);
        }
    }
}

for ($i = 0; $i < count($game2); $i++) {
    echo $game2[$i];
    echo ", ";
}
Run Code Online (Sandbox Code Playgroud)

我期待结果是:

1, 3, 5, 7,
Run Code Online (Sandbox Code Playgroud)

但是,我得到:

1, 1, 1, 1, 3, 3, 3, 3, …
Run Code Online (Sandbox Code Playgroud)

php arrays loops for-loop nested-loops

1
推荐指数
2
解决办法
117
查看次数

golfscript嵌套while循环

嵌套在golfscript中的循环中,或者我不知道如何使用它们?

我希望将Q从5迭代到0,并且对于每次迭代,将Z从10迭代到0.单循环分别运行良好,它们看起来是自包含的(不依赖于操作之间的堆栈):

5:Q;
{"Q:"Q+ p Q}
{
  Q 1- :Q;
}while

10:Z;{"Z:"Z+ p Z}{Z 1- :Z;}while
Run Code Online (Sandbox Code Playgroud)

输出:

"Q:5"
"Q:4"
"Q:3"
"Q:2"
"Q:1"
"Q:0"
"Z:10"
"Z:9"
"Z:8"
"Z:7"
"Z:6"
"Z:5"
"Z:4"
"Z:3"
"Z:2"
"Z:1"
"Z:0"
Run Code Online (Sandbox Code Playgroud)

但是如果我把Z循环放在Q循环中,我会得到奇怪的结果:

5:Q;
{"Q:"Q+ p Q}
{
  10:Z;{"Z:"Z+ p Z}{Z 1- :Z;}while

  Q 1- :Q;
}while
Run Code Online (Sandbox Code Playgroud)

输出:

"Q:5"
"Z:10"
"Z:9"
"Z:8"
"Z:7"
"Z:6"
"Z:5"
"Z:4"
"Z:3"
"Z:2"
"Z:1"
"Z:0"
"Z:0"
Run Code Online (Sandbox Code Playgroud)

基于Z打印输出两次,似乎只有一个当前条件块,并且任何执行"while"都会覆盖它.

无论如何,我将如何在golfscript中完成这一壮举?

loops while-loop nested-loops golfscript

1
推荐指数
1
解决办法
316
查看次数

OpenMP嵌套循环并行

我使用OpenMP,我遇到了错误结果的问题.

这是代码:

    #pragma omp parallel shared(L,nthreads,chunk) private(tid,i,j){
        tid = omp_get_thread_num();
        if (tid == 0)
        {
            nthreads = omp_get_num_threads();
            printf("Starting matrix multiple example with %d threads\n",nthreads);
            printf("Initializing matrices...\n");
        }

        #pragma omp for schedule (static, chunk) 
        for( i=0; i<SIZE_A;i++){
            for( j=0; j<SIZE_B;j++){
                if(A[i]==B[j]){
                    if(i==0 || j==0)
                        L[i][j]=1;
                    else
                        L[i][j] = L[i-1][j-1] + 1;
                }
                // or reset the matching score to 0
                else
                    L[i][j]=0;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

你怎么想,为什么我得到了结果?我应该改变什么?

非常感谢!

c parallel-processing loops openmp nested-loops

1
推荐指数
1
解决办法
1159
查看次数

将Array方法与嵌套(多维)数组一起使用?

使用嵌套数组的Array方法时遇到问题.

var map = [
        ["Blank", "Blank", "Blank"],
        ["Blank", "Player", "Blank"],
        ["Blank", "Blank", "Blank"]
];

for (i=0; i<map.length; i++) {
        for (j=0; j<map[i].length; j++) {
            var playerY = map[i][j].indexOf("Player");
        }
}

console.log(playerY);
Run Code Online (Sandbox Code Playgroud)

这将始终记录-1,这是我知道一个错误.虽然我认为我的问题是使用嵌套数组.它也可能是我使用.indexOf()的方式的问题,或者我循环遍历数组的方式.谢谢你的帮忙.任何建议将不胜感激!:)

编辑:谢谢你的帮助.我最终改变了很多东西,而不是一起使用.indexOf()方法.这就是我最终做的事情.

var map = [
        ["Blank", "Blank", "Blank"],
        ["Blank", "Player", "Blank"],
        ["Blank", "Blank", "Blank"]
];

for (x = 0; x < map.length; x++) {
    for (y = 0; y < map[x].length; y++) {
        if (map[x][y] == "Player") {
            console.log("(" + x.toString() + ", " + y.toString() + …
Run Code Online (Sandbox Code Playgroud)

javascript nested-loops multidimensional-array

1
推荐指数
1
解决办法
92
查看次数

字典理解嵌套循环不按计划工作

我有以下python代码:

print {a:b for a in [1, 2] for b in [3, 4, 5]}
Run Code Online (Sandbox Code Playgroud)

我希望能给我这样的东西:

{1:3, 1:4, 1:5, 2:3, 2:4, 2:5}
Run Code Online (Sandbox Code Playgroud)

但它反过来给了我这个:

{1: 5, 2: 5}
Run Code Online (Sandbox Code Playgroud)

我也有逆转,如环试了一下这里建议:

print {a:b for b in [3, 4, 5] for a in [1, 2]}
Run Code Online (Sandbox Code Playgroud)

但它仍然给了我错误的答案.而且我也在列表中尝试了同样的理解:

print [(a, b) for a in [1, 2] for b in [3, 4, 5]]
Run Code Online (Sandbox Code Playgroud)

这与预期完全一致.

我对字典有什么看法?

仅供参考,python命令返回:

Python 2.7.4 (default, Apr 19 2013, 18:32:33) 
[GCC 4.7.3] on linux2
Run Code Online (Sandbox Code Playgroud)

python dictionary nested-loops python-2.7

1
推荐指数
1
解决办法
122
查看次数

嵌套循环的顺序是否对速度有影响

假设我有两个数组arrayOne并且arrayTwo在哪里arrayOne.length != arrayTwo.length(假设两个List具有不同的类似情况size()).以下任何一种都提供了速度优势吗?

java arrays performance loops nested-loops

1
推荐指数
1
解决办法
968
查看次数

比拥有3个嵌套for循环更好的方法来获取我需要的数据

我可以用我当前正在做的方式建立一本字典,但我想知道是否有更好更快的方法.我正在尝试使用LINQ SelecMany语句,但遇到了麻烦.

var replyChildren = reply.Children[0];
Dictionary<int, string> slowLog = new Dictionary<int, string>();
for (int i = 0; i < replyChildren.Children.Count(); i++)
{
    var ithReplyChildren = replyChildren.Children[i];
    for (int j = 0; j < 4; j++)
    {
        if (j == 3)
        {
            var jthReplyChildren = ithReplyChildren.Children[j];
            for (int k = 0; k < jthReplyChildren.Count; k++)
            {
                serverInfo += jthReplyChildren.Children[k].TextData; //+ "::";
            }
            break;
        }
        serverInfo += ithReplyChildren.Children[j].TextData + ":";
    }
    slowLog.Add(i, serverInfo);
    serverInfo = "";
}
Run Code Online (Sandbox Code Playgroud)

回复将有孩子在里面(第0个元素)然后在那些将有孩子的内部,依此类推.第二个for循环将只有4个孩子,第四个将不得不更深入一步并得到孩子们.

c# linq nested-loops

1
推荐指数
1
解决办法
524
查看次数

以用户给出的一位数打印所有的narcisistic数字

我试图打印用户输入的所有数字的自恋数字.

例如,对于输入3,程序应该打印:153, 370, 371, 407.现在由于某种原因而不是打印数字,它没有打印任何东西,程序卡住了.

#include <stdio.h>
#include <math.h>

int main() {
    int digit, a, c = 0;
    unsigned long long int count, b, sum;
    printf("Enter digits to check narcisistic: ");
    scanf("%d", &digit);
    count = pow(10, digit - 1);
    if (digit > 2) {
        while (count < pow(10, digit)) {
            b = count;
            sum = 0;
            while (count >= 1) {
                a = b % 10;
                b /= 10;
                sum += pow(a, digit);
            }
            if (sum …
Run Code Online (Sandbox Code Playgroud)

c while-loop nested-loops

1
推荐指数
1
解决办法
58
查看次数

循环嵌套循环(在R或Stata中)

我有一个60个尺寸的嵌套循环,也就是说,我相互嵌套60个循环。在Stata中,MWE如下所示:

forvalues i = 1/60 {
    forvalues j = 1/60 {
        forvalues k = 1/60 {
            forvalues l = 1/60 {
                ... imagine the 56 remaining loops here
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

R中的等效项是:

for(i in 1:60) {
    for(j in 1:60) {
        for(k in 1:60) {
            for(l in 1:60) {
                ... imagine the 56 remaining loops here
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里的目的是避免在我的代码中键入所有60个级别,而是为循环结构本身创建一个循环。这个问题显得微不足道。但是由于某种原因,我正在努力提出解决方案。

感谢您的任何建议。

附加信息:

我有一个包含60个解释变量的数据集,并希望对这些变量的每种可能组合进行回归分析。更具体地说,我分别对所有60个解释变量运行因变量的单变量回归并计算某些条件。然后,将第二个回归变量添加到估计方程中,然后再次计算标准。即reg DependentVar ExplVar1 ExplVar2reg DependentVar ExplVar1 ExplVar3,..., reg DependentVar ExplVar60 ExplVar59。取决于计算的标准,该回归树的分支可以提前,也可以终止。例如,第一个分支reg …

loops r nested-loops stata

1
推荐指数
1
解决办法
187
查看次数