誰かがこれに対する解決策を提案できますか?
複数のリストがあります。 これらのリストの値の可能なすべての組み合わせを繰り返す必要があります。 問題は、値を取得するリストの数が実行時に決定されることです。 したがって、2つのリストに対して2つのネストされた「for」ループを記述してから、3つのリストに対して3つのネストされた「for」ループを記述することはできません。 どういうわけか、リストの名前を(おそらくリスト自体として)渡して解析し、リスト内のすべてのリスト/値を反復処理する必要があります。
何か案は?
JMPが再帰が深すぎると判断する前に、これがどれだけ深くなるかはわかりません。最初にテストしてください...リストが多すぎる場合は、スタックを使用した回避策が少し少なくなります。 ただし、各リストに少なくとも2つの要素がある場合は、最初に時間がなくなると思います。
// 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"}
};