누군가 이것에 대한 해결책을 제안할 수 있습니까?
목록이 여러 개 있습니다. 이 목록에서 가능한 모든 값 조합을 반복해야 합니다. 문제는 내가 값을 가져오는 목록의 수가 런타임에 결정된다는 것입니다. 따라서 두 개의 목록에 대해 두 개의 중첩된 'for' 루프를 작성한 다음 세 개의 목록에 대해 세 개의 중첩된 'for' 루프를 작성할 수 없습니다. 어떻게든 목록의 이름을 전달\구문 분석(아마도 목록 자체로)한 다음 목록의 모든 목록\값을 반복해야 합니다.
어떤 아이디어?
JMP가 재귀가 너무 깊다고 생각하기 전에 이것이 얼마나 깊이 갈 수 있는지 잘 모르겠습니다. 따라서 먼저 테스트하십시오...목록이 너무 많은 경우 스택을 사용하는 해결 방법이 약간 덜 좋습니다. 그러나 각 목록에 적어도 두 개의 요소가 있으면 먼저 시간이 부족할 것입니다.
// the variable length list of variable length lists
L = {{"a", "b"}, {"c"}, {"d", "e", "f"}, {"g", "h", "i", "j"}};
result = {}; // a place to accumulate the results
p = {}; // a single result
// a recursive function to "nest the for loops"
generate = Function( {i}, // the "layer"
{j}, // the local for-loop counter
If( i > N Items( L ),
result[N Items( result ) + 1] = p; // you could just use p at this point
Return();
);
For( j = 1, j <= N Items( L[i] ), j += 1, // go through all the items at this layer
Insert Into( p, L[i][j] ); // add each to the single result
Recurse( i + 1 ); // go to next layer
Remove From( p ); // remove from the single result
);
);
generate( 1 ); // run the recursive function
Show( result );결과 = {
{"a", "c", "d", "g"},
{"a", "c", "d", "h"},
{"a", "c", "d", "i"},
{"a", "c", "d", "j"},
{"a", "c", "e", "g"},
{"a", "c", "e", "h"},
{"a", "c", "e", "i"},
{"a", "c", "e", "j"},
{"a", "c", "f", "g"},
{"a", "c", "f", "h"},
{"a", "c", "f", "i"},
{"a", "c", "f", "j"},
{"b", "c", "d", "g"},
{"b", "c", "d", "h"},
{"b", "c", "d", "i"},
{"b", "c", "d", "j"},
{"b", "c", "e", "g"},
{"b", "c", "e", "h"},
{"b", "c", "e", "i"},
{"b", "c", "e", "j"},
{"b", "c", "f", "g"},
{"b", "c", "f", "h"},
{"b", "c", "f", "i"},
{"b", "c", "f", "j"}
};