Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting Mathematica,具有公共值的组对_Sorting_Wolfram Mathematica - Fatal编程技术网

Sorting Mathematica,具有公共值的组对

Sorting Mathematica,具有公共值的组对,sorting,wolfram-mathematica,Sorting,Wolfram Mathematica,我试图使用Mathematica中的GatherBy函数来获取列表a={{1,4},{2,3},{1,5},{2,5},{3,4},{6,8},{6,7},{7,8}中的对,并按包含值1的对排序。理想情况下,输出将类似于output={{{{1,4}、{1,5}、{2,3}、{2,5}、{3,4}、{6,8}、{6,7}、{7,8}}或类似的东西,其中输出中的第一个元素是a中包含1的所有元素的列表,第二个元素包含不包含1的所有对。回答注释中的问题,排序确保所需顺序的结果: GatherBy[a,

我试图使用Mathematica中的
GatherBy
函数来获取列表
a={{1,4},{2,3},{1,5},{2,5},{3,4},{6,8},{6,7},{7,8}
中的对,并按包含值1的对排序。理想情况下,输出将类似于
output={{{{1,4}、{1,5}、{2,3}、{2,5}、{3,4}、{6,8}、{6,7}、{7,8}}
或类似的东西,其中输出中的第一个元素是
a
中包含1的所有元素的列表,第二个元素包含不包含1的所有对。

回答注释中的问题,
排序
确保所需顺序的结果:

GatherBy[a, #[[1]] == 1 || #[[2]] == 1 &]
a = {{1, 4}, {2, 3}, {1, 5}, {2, 5}, {3, 4}, {6, 8}, {6, 7}, {7, 8}};
SortBy[GatherBy[a, MemberQ[#, 2] &], !MemberQ[First@#, 2] &]
另一种方法:

Reap[Sow[ # , MemberQ[#, 2]  ] & /@ a, {True, False}] // Last
任何一种都会产生:

{{{{2, 3}, {2, 5}}}, {{{1, 4}, {1, 5}, {3, 4}, {6, 8}, {6, 7}, {7, 
8}}}}

回答注释中的q,
排序
结果以确保所需顺序:

a = {{1, 4}, {2, 3}, {1, 5}, {2, 5}, {3, 4}, {6, 8}, {6, 7}, {7, 8}};
SortBy[GatherBy[a, MemberQ[#, 2] &], !MemberQ[First@#, 2] &]
另一种方法:

Reap[Sow[ # , MemberQ[#, 2]  ] & /@ a, {True, False}] // Last
任何一种都会产生:

{{{{2, 3}, {2, 5}}}, {{{1, 4}, {1, 5}, {3, 4}, {6, 8}, {6, 7}, {7, 
8}}}}

谢谢你,永毅。我发现这样做效果更好,
b=GatherBy[a,MemberQ[#,1]&]
,因为我将组合所有包含1的集合,因此它们不会在以后的过程中简单地成对出现。对不起,漏掉了那部分。我猜作为一个推论,当我开始基于值4进行分组时,我使用了一个类似的命令
b=GatherBy[a,MemberQ[#,4]&]Output:{{{{{6,8},{6,7},{7,8},{1,4,5},{2,3,4,5}}
但是我希望包含for的列表集首先出现。有什么建议吗?谢谢你,永毅。我发现这样做效果更好,
b=GatherBy[a,MemberQ[#,1]&]
,因为我将组合所有包含1的集合,因此它们不会在以后的过程中简单地成对出现。对不起,漏掉了那部分。我猜作为一个推论,当我开始基于值4进行分组时,我使用了一个类似的命令
b=GatherBy[a,MemberQ[#,4]&]Output:{{{{{6,8},{6,7},{7,8},{1,4,5},{2,3,4,5}}
但是我希望包含for的列表集首先出现。有什么建议吗?