找到满足特定条件的对的有效方法

发布于 2024-10-03 02:52:32 字数 982 浏览 1 评论 0原文

AB 为列表。我要找到所有对 {x,y} ,其中 x 位于 A 中,y 位于 < code>B 并且某些条件 Cond[x,y] 为真。这是我想出的,但它相当麻烦,我怀疑有更好的方法

AllPairs[A_, B_, Cond_] := Module[{i, k, C, Cp},
  C = {};
  For[i = 1, i <= Length[A], i++,
  Cp = Select[B, Cond[A[[i]], #] &];
  C = C~Join~Table[{A[[i]], Cp[[k]]}, {k, 1, Length[Cp]}];
 ];
Return[C];
]

例如

In[1]:= AllPairs[{1, 2, 3, 4}, {3, 4, 5}, EvenQ[#1 + #2] &]
Out[1]:= {{1, 3}, {1, 5}, {2, 4}, {3, 3}, {3, 5}, {4, 4}}

我的这段代码的另一个问题是它不容易泛化。我想要一个函数,它接受列表 A1, A2,...,An 和一些条件 Cond[x___] 并输出所有 n 元组 { x1,x2,...,xn} 其中 x1 位于 A1 ... xn 位于 Cond[x1,x2,...,xn] 为 true。

最后,是否有一个内置函数可以计算两个或多个列表的笛卡尔积

谢谢!

Let A and B be lists. I was to find all pairs {x,y} for which x is in A, y is in B and some condition Cond[x,y] is true. This is what I've come up with, but its quite cumbersome and I suspect there is a better way

AllPairs[A_, B_, Cond_] := Module[{i, k, C, Cp},
  C = {};
  For[i = 1, i <= Length[A], i++,
  Cp = Select[B, Cond[A[[i]], #] &];
  C = C~Join~Table[{A[[i]], Cp[[k]]}, {k, 1, Length[Cp]}];
 ];
Return[C];
]

For example

In[1]:= AllPairs[{1, 2, 3, 4}, {3, 4, 5}, EvenQ[#1 + #2] &]
Out[1]:= {{1, 3}, {1, 5}, {2, 4}, {3, 3}, {3, 5}, {4, 4}}

My other problem with this code is that it doesn't generalize easily. I would like to have a function which takes in lists A1, A2,...,An and some condition Cond[x___] and outputs all n tuples {x1,x2,...,xn} for which x1 is in A1 ... xn is in An and Cond[x1,x2,...,xn] is true.

And finally, is there a built in function which computes the cartesian product of two or more lists?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

找个人就嫁了吧 2024-10-10 02:52:32

如果您需要检查所有对(即没有对称性可用于减少问题),那么最简单的可能是 SelectTuples

allPairs[a_,b_,cond_]:=Select[Tuples@{a,b},cond@@#&];

这就是我认为的想要:

a=Range[4]; b=Range[3,5];
allPairs[a,b,EvenQ[#1+#2]&]
Out[37]= {{1,3},{1,5},{2,4},{3,3},{3,5},{4,4}}

关于生成对的更多工具,请查找 TuplesOuter

Tuples[a,2] (* 2-tuples with entries from a *)
Tuples[{a,b}] (* 2-tuples with firt (2nd) entry from a (b) *)
Outer[List,a,b] (* cartesian product *)

希望这会有所帮助。

If you need to check all pairs (i.e. there is no symmetry to use for reducing the problem), then the simplest is probably Select and Tuples:

allPairs[a_,b_,cond_]:=Select[Tuples@{a,b},cond@@#&];

Which does what I think you want:

a=Range[4]; b=Range[3,5];
allPairs[a,b,EvenQ[#1+#2]&]
Out[37]= {{1,3},{1,5},{2,4},{3,3},{3,5},{4,4}}

As for more tools for generating pairs look up Tuples and Outer:

Tuples[a,2] (* 2-tuples with entries from a *)
Tuples[{a,b}] (* 2-tuples with firt (2nd) entry from a (b) *)
Outer[List,a,b] (* cartesian product *)

Hope this helps.

断肠人 2024-10-10 02:52:32

对于这样的问题,我个人喜欢使用案例并附加一个条件。不过我不确定效率如何。

lst1 = {1, 2, 3, 4};
lst2 = {3, 4, 5};
Cases[Tuples[{lst1, lst2}], {x_, y_} /; EvenQ[x + y]]

我发现这种方法简单且通用,至少对于小型列表(我使用的类型!)

Cases[Tuples[{lst1, lst2}], {x_ /; EvenQ[x], y_ /; OddQ[y]}]

For problems like this I personally like to use Cases and attach a condition. I'm not sure about the efficiency, though.

lst1 = {1, 2, 3, 4};
lst2 = {3, 4, 5};
Cases[Tuples[{lst1, lst2}], {x_, y_} /; EvenQ[x + y]]

I find the approach simple and versatile, at least for small lists (of the type I work with!)

Cases[Tuples[{lst1, lst2}], {x_ /; EvenQ[x], y_ /; OddQ[y]}]
青春有你 2024-10-10 02:52:32

另一种解决方案使用 ReplaceList ——它比 Janus 的答案慢大约 4 倍(比原始方法慢 3 倍),但内存效率可能更高。

In[1]:= allPairs1[a_,b_,cond_]:=Select[Tuples@{a,b},cond@@#&];
In[2]:= allPairs2[a_,b_,cond_]:=ReplaceList[{a,b},
                                  {{___,x_,___},{___,y_,___}}/;cond[x,y]:>{x,y}]

In[3]:= aa=RandomInteger[{0,10^5},{1000}];
In[4]:= bb=RandomInteger[{0,10^5},{1000}];

In[5]:= test1=allPairs1[aa,bb,EvenQ[#1+#2]&];//Timing
Out[5]= {4.99,Null}

In[6]:= test2=allPairs2[aa,bb,EvenQ[#1+#2]&];//Timing
Out[6]= {19.12,Null}

In[7]:= test1==test2
Out[7]= True

An alternate solution uses ReplaceList -- it is about 4 times slower than Janus' answer (and 3 times slower than the original method), but probably more memory efficient.

In[1]:= allPairs1[a_,b_,cond_]:=Select[Tuples@{a,b},cond@@#&];
In[2]:= allPairs2[a_,b_,cond_]:=ReplaceList[{a,b},
                                  {{___,x_,___},{___,y_,___}}/;cond[x,y]:>{x,y}]

In[3]:= aa=RandomInteger[{0,10^5},{1000}];
In[4]:= bb=RandomInteger[{0,10^5},{1000}];

In[5]:= test1=allPairs1[aa,bb,EvenQ[#1+#2]&];//Timing
Out[5]= {4.99,Null}

In[6]:= test2=allPairs2[aa,bb,EvenQ[#1+#2]&];//Timing
Out[6]= {19.12,Null}

In[7]:= test1==test2
Out[7]= True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文