1

How to delete the redundant parts of each association according to the corresponding intersection of parts in the key list.

Select[<|{x1, 
    x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", 
     "w"}}, {x2, 
    x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}}, {x3, x6, 
    x1} -> {{"t", "t", "a"}, {"h", "u", "r"}, {"a", "t", "l"}}|>, f]

That is to realize the calculation results in the figure below:

enter image description here

In the end, all parts of each key do not contain different values. What can I do to achieve this goal?

Further explanation of this problem:

First, we take the intersection of the values corresponding to the associated key:

assoc = <|{x1, 
    x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", 
     "w"}}, {x2, 
    x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}}, {x3, x6, 
    x1} -> {{"t", "t", "a"}, {"h", "u", "r"}, {"a", "t", "l"}}|>
Merge[KeyValueMap[Thread[#1 -> #2\[Transpose]] &, assoc], 
 Intersection @@ # &]

The result of the above code is <|x1->{"a", "r"},x2->{"k", "t"},x6->{"t", "u"},x3->{"a", "h", "t"}|>. so we know that x1 should be selected from {"a", "r"}, x2 should be selected from {"k", "t"}, x3 should be selected from {"a", "h", "t"}, x6 should be selected from {"t", "u"}.

enter image description here

Taking the first group of association selection as an example, only {"a","t"} or {"r","t"} of the values of the key {x1, x2} meet the above selection rules at the same time. Therefore, x2 can only take "t". Further analysis shows that x6 can only take "t". In the third group of relationships, only {"t", "t", "a"} can satisfy the requirement that x1 can only take "a" or "t", x6 can only take "t" and x3 can only be selected from {"a", "H", "t"} in the same time. So the end result is <|{x1, x2} -> {"a", "t"}, {x2, x6} -> {"t", "t"}, {x3, x6, x1} -> {"t", "t", "a"}|>.

enter image description here

But I can't implement this process in code coherently. I need help.


Add:

Thank you very much for kglr's help, but when I applied your code to this problem, a small problem occurred.

When I reassign x as following:

 assoc = Association[
  s1 = Merge[
    Map[Rule[{x1, x2}, #] &, 
     Flatten[StringCases[DictionaryLookup["w" ~~ _ ~~ "is" ~~ _], 
       "w" ~~ x1_ ~~ "is" ~~ x2_ :> {x1, x2}], 1]], Identity],
  s2 = Merge[
    Map[Rule[{x1, x3, x4, x8}, #] &, 
     Flatten[StringCases[DictionaryLookup[_ ~~ _ ~~ _ ~~ "l" ~~ _], 
       x1_ ~~ x3_ ~~ x4_ ~~ "l" ~~ x8_ :> {x1, x3, x4, x8}], 1]], 
    Identity],

  s3 = Merge[
    Map[Rule[{x2, x6}, #] &, 
     Flatten[StringCases[DictionaryLookup[_ ~~ "o" ~~ _], 
       x2_ ~~ "o" ~~ x6_ :> {x2, x6}], 1]], Identity],

  s4 = Merge[
    Map[Rule[{x4, x5, x6}, #] &, 
     Flatten[StringCases[DictionaryLookup[_ ~~ "n" ~~ _ ~~ _ ~~ _], 
       x4_ ~~ "n" ~~ x5_ ~~ x6_ :> {x4, x5, x6}], 1]], Identity],

  s5 = Merge[
    Map[Rule[{x7, x8, x9}, #] &, 
     Flatten[StringCases[DictionaryLookup[_ ~~ _ ~~ "c" ~~ _], 
       x7_ ~~ x8_ ~~ "c" ~~ x9_ :> {x7, x8, x9}], 1]], Identity]]

solution = 
  Association[
    First@Solve[
      And @@ Or @@@ 
        KeyValueMap[
          Map[Apply[And]@*Thread]@
            Thread[Unevaluated@Equal[##], List, {2}] &]@#, 
      Union @@ Keys[#]]] &;

solution@assoc

I can't get the result after running the above code.

  • 1
    why not map First on the association: i.e., assoc = <|{x1, x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", "w"}}, {x2, x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}}, {x3, x6, x1} -> {{"t", "t", "a"}, {"h", "u", "r"}, {"a", "t", "l"}}|>; First /@ assoc? – kglr Mar 25 '20 at 08:02
  • @kglr He maybe need to break the order of assoc ,for example assoc = <|{x1, x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", "w"}}, {x2, x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}}, {x3, x6, x1} -> {{"h", "u", "r"}, {"t", "t", "a"}, {"a", "t", "l"}}|>, the result is still <|{x1,x2}->{a,t},{x2,x6}->{t,t},{x3,x6,x1}->{t,t,a}|>. – A little mouse on the pampas Mar 25 '20 at 08:52
  • @PleaseCorrectGrammarMistakes, it is not clear to me what the desired result would be for your example. Perhaps bmc013 should update with a clarification. – kglr Mar 25 '20 at 11:27

1 Answers1

1
assoc = <|{x1,  x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", "w"}}, 
       {x2,  x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}},
       {x3, x6, x1} -> {{"t", "t", "a"}, {"h", "u", "r"}, {"a", "t", "l"}}|>;

desired = <|{x1, x2} -> {"a", "t"}, {x2, x6} -> {"t", "t"}, 
      {x3, x6, x1} -> {"t", "t", "a"}|>;

solution = Association[First @ Solve[And @@ Or @@@ KeyValueMap[Map[Apply[And]@*Thread]@
     Thread[Unevaluated@Equal[##], List, {2}] &]@#, Union @@ Keys[#]]] &;

solution @ assoc
 <|x1 -> "a", x2 -> "t", x3 -> "t", x6 -> "t"|>
Association[# -> Lookup[solution @ assoc, #] & /@ Keys[assoc]]
 <|{x1, x2} -> {"a", "t"}, {x2, x6} -> {"t", "t"}, {x3, x6, x1} -> {"t", "t", "a"}|>
% == desired
True
kglr
  • 394,356
  • 18
  • 477
  • 896
  • But when assoc = <|{x1, x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", "w"}}, {x2, x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}}, {x3, x6, x1} -> {{"h", "u", "r"}, {"a", "t", "l"}, {"t", "t", "a"}}|>, the result is <|{x1,x2}->{a,t},{x2,x6}->{t,t},{x3,x6,x1}->{h,t,a}|>, but in fact, the result should still be <|{x1, x2} -> {"a", "t"}, {x2, x6} -> {"t", "t"}, {x3, x6, x1} -> {"t", "t", "a"}|>. – A little mouse on the pampas Mar 25 '20 at 11:07
  • 2
    @PleaseCorrectGrammarMistakes, i am not sure about the " in fact, the result should still be ..." part. Waiting for bmc013's clarification. – kglr Mar 25 '20 at 11:31
  • @kglr I've updated the question. If assoc changes the order as required, for example, to assoc = <|{x1, x2} -> {{"a", "t"}, {"h", "k"}, {"h", "t"}, {"r", "t"}, {"s", "w"}}, {x2, x6} -> {{"t", "t"}, {"h", "k"}, {"k", "u"}, {"r", "t"}}, {x3, x6, x1} -> {{"h", "u", "r"}, {"a", "t", "l"}, {"t", "t", "a"}}|>, the result should still be <|{x1, x2} -> {"a", "t"}, {x2, x6} -> {"t", "t"}, {x3, x6, x1} -> {"t", "t", "a"}|>. This is my problem to achieve the goal. If the description of the question is still unclear, please @ me. –  Mar 25 '20 at 12:24
  • 1
    @bmc013, thank you for the clarification. Please see the new version. – kglr Mar 25 '20 at 13:20
  • 1
    @kglr Thank for your help, I'm going to solve this problem with your method, but I have some difficulties. I've updated the problem description. –  Mar 25 '20 at 23:27
  • 1
    @bmc013, the problem size seems to be too large for Reduce ( I had to abort after 5 minutes). – kglr Mar 26 '20 at 06:27
  • @kglr Can you find an algorithm to calculate this puzzle quickly? –  Mar 26 '20 at 22:57