パターンマッチングを使用してリスト内の要素を強調表示する

Nov 27 2020

以下から開始list

list = {{a, b, c}, {d, e, f}, {g, h, i}, {b, c, d}, {c, a, m}, {c, d, n}};

list交点が2より大きい要素を強調表示したいと思います。

次のコードは、私が望む結果を得ることができません:

list //. 
   {{a___, x:{_, _, _}, b___, y:{_, _, _}, c___} /; 
      Length@Intersection[x, y] >= 2 :> 
         {a, Style[x, Gray], b, Style[y, Gray], c}}

望ましい結果は

私も検討Gatherしましたが、リストの順番が変わります。

更新:
エレガントではなく、方法を考えました

list //. {a___,x:({_,_,_}|F[{_,_,_}]),b___,y:({_,_,_}),c___}/;
  Length[Intersection[x/.F->Identity,y]]>=2:>{a,F@x,b,F@y,c}
% /. F->Highlighted

回答

3 kglr Nov 27 2020 at 00:57
rg = RelationGraph[UnsameQ @ ## && Length@Intersection[##] >= 2 &, list]

hl = VertexList @ EdgeList @ rg
{{a, b, c}, {b, c, d}, {c, a, m}, {c, d, n}}
list /. x : Alternatives @@ hl :> Style[x, Gray] 

list /. x : Alternatives @@ hl :> Highlighted[x, BaseStyle -> Red]

HighlightGraph[rg, hl]

ConnectedComponents複数の頂点を持つコンポーネントを使用して選択することもできます。

ccs = Select[Length @ # >= 2 &] @ ConnectedComponents[rg]
 {{{a, b, c}, {b, c, d}, {c, a, m}, {c, d, n}}}
list /. x : Alternatives @@ # :> Highlighted[x, BaseStyle -> Red]& /@ ccs

2 MarcoB Nov 27 2020 at 00:44
ClearAll[formatList]
formatList[list_] := Module[{rules},
  rules =
    AssociationThread[
      list -> (If[Max[#] >= 2, Gray, Black] & /@ 
        Function[{element}, 
          Length@Intersection[element, #] & /@ 
           Complement[list, {element}]] /@ list)
    ];
  Style[#, rules[#]] & /@ list
]

formatList[list]

1 kglr Nov 27 2020 at 13:49

のネストを回避するOPのソリューションの変形Highlighted

list //. {a___, x : ({_, _, _} | Highlighted[{_, _, _}, ___]), b___, 
    y : ({_, _, _}), c___} /; Length[Intersection[x /. Highlighted -> (# &), y]] >= 2 :> 
   {a, Highlighted[x /. Highlighted -> (# &)], b, Highlighted@y, c}

を使用した同じアプローチStyle

list //. {a___, x : ({_, _, _} | Style[{_, _, _}, ___]), b___, 
    y : ({_, _, _}), c___} /; Length[Intersection[x /. Style -> (# &), y]] >= 2 :> 
   {a, Style[x /. Style -> (# &), Gray], b, Style[y /. Style -> (# &), Gray], c}

1 cvgmt Nov 27 2020 at 16:02

簡単な方法は、まだGatherインデックスを使用して並べ替えることだと思います。ここでは、一般的な状況を扱います。

SeedRandom[400];
list = Table[RandomSample[Alphabet[], 3], 40];
newlist = Thread[Range[Length@list] -> list];
result = Gather[newlist, 
   Length[Intersection[Last@#1, Last@#2]] >= 2 &];
keys = Keys /@ result;
keyc = Thread[keys -> RandomColor[Length@keys]]
map[j_] := 
  MapAt[Style[#, Last@keyc[[j]], Bold] &, List /@ First@keyc[[j]]];
fig = Composition[Sequence @@ Table[map[j], {j, 1, Length@keyc}]]@
  list
Grid[Partition[fig, 8], Frame -> All]

1 kglr Nov 27 2020 at 17:52
list /. x : {__Symbol} /; 
  Max[Length[Intersection[x, #]] & /@ DeleteCases[list, x]] >= 2 :> 
    Style[x, Gray] 

を使用する方法GatherBy

gb = Join @@ Select[Length@# > 1 &]@
   GatherBy[list, Function[x, Max[Length[Intersection[x, #]] & /@ DeleteCases[x][list]]]]
 {{a, b, c}, {b, c, d}, {c, a, m}, {c, d, n}}
list /. x : Alternatives @@ gb :> Style[x, Gray]

なぜGather機能しないのですか:

より簡単な例をとると:

list2 = Partition[Range@5, 3, 1];
GatherBy[list2, Function[x, Max[Length[Intersection[x, #]] & /@ 
  DeleteCases[x][list2]] >= 2]]
 {{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}}
Gather[list2, Length[Intersection[##]] >= 2 &]
{{{1, 2, 3}, {2, 3, 4}},
 {{3, 4, 5}}}

Gather入力リストのすべてのペアでテスト機能を実行するわけではありません。テスト関数Trueがペアに対して評価する場合{p1, p2}p1およびp2がグループ化されるように)、ペア{p1, p3}はテストさ{p2, p3}れますが、Trace出力に示されているようにスキップされます。

Trace[Gather[list2, Length[Intersection[##]] >= 2 &]] // Rest // Rest // Column

注トリプルもの{2, 3, 4}{3, 4, 5}されている比較していないので({2, 3, 4}れる既に集め}。

kglr Nov 27 2020 at 20:51

さらに別のアプローチはUnion、条件を満たす2つのサブセットを使用することです。

highlighted = Union @@ Select[Length[Intersection @@ #] >= 2 &] @ Subsets[list, {2}]
{{a, b, c}, {b, c, d}, {c, a, m}, {c, d, n}}
list /. x : Alternatives @@ highlighted :> Style[x, Gray]