12
$\begingroup$

I have two lists:

list={{1,2,3,5},{5,3,9,11,12},{5,9,10,16}};
list2={{7,89},{96,5},{-6,-98}};

This is the expected result.

{{1->{7,89},2->{7,89},3->{7,89},5->{7,89}},
 {5->{96,5},3->{96,5},9->{96,5},11->{96,5},12->{96,5}},
 {5->{-6,-98},9->{-6,-98},10->{-6,-98},16->{-6,-98}}}

This is my current try.

Thread /@ 
 Thread[Rule[list, 
   MapIndexed[ConstantArray[list2[[First[#2]]], Length[#1]] &, list]]]
$\endgroup$

8 Answers 8

14
$\begingroup$
Tuples /@ Thread[list -> List /@ list2]
{
 {1 -> {7, 89}, 2 -> {7, 89}, 3 -> {7, 89}, 5 -> {7, 89}},
 {5 -> {96, 5}, 3 -> {96, 5}, 9 -> {96, 5}, 11 -> {96, 5}, 12 -> {96, 5}},
 {5 -> {-6, -98}, 9 -> {-6, -98}, 10 -> {-6, -98}, 16 -> {-6, -98}}
}
$\endgroup$
6
  • $\begingroup$ Good lesson to me.Thanks. $\endgroup$
    – yode
    Commented Dec 25, 2016 at 18:58
  • $\begingroup$ @yode You're welcome :^) $\endgroup$
    – Mr.Wizard
    Commented Dec 25, 2016 at 19:01
  • $\begingroup$ I mean,your usage of Tuples and List/@ astonish me. :) $\endgroup$
    – yode
    Commented Dec 25, 2016 at 19:04
  • $\begingroup$ @yode I was just having good-natured fun with your typo. I am glad you learned from this example. Quite a few functions work on heads other than List which is quite handy, e.g. (1312) $\endgroup$
    – Mr.Wizard
    Commented Dec 26, 2016 at 3:13
  • $\begingroup$ It's not a typo.I just cannot hold the temper of my English sometimes. :) $\endgroup$
    – yode
    Commented Dec 26, 2016 at 3:26
10
$\begingroup$
MapIndexed[# -> list2[[#2[[1]]]] &, list, {2}]

or

ReplacePart[list, {i_, j_} :> list[[i, j]] -> list2[[i]]]
$\endgroup$
1
  • $\begingroup$ Wow,thanks.This is very concise method,too. $\endgroup$
    – yode
    Commented Dec 25, 2016 at 8:08
9
$\begingroup$
Thread /@ Thread[list -> Hold /@ list2] // ReleaseHold
Thread /@ Thread[list → Unevaluated /@ list2] /. a_ → b_ -> a -> b
Thread /@ Thread[list -> $ /@ list2] /. $ -> (# &)

As to the reason why I used RightArrow as a medium in the second solution, you may want to read this post.

$\endgroup$
3
  • $\begingroup$ +1)Impressive...BTY,how do you type that second $\to$? $\endgroup$
    – yode
    Commented Dec 25, 2016 at 7:31
  • $\begingroup$ @yode I just transformed \[RightArrow] with M.SE editor :) . $\endgroup$
    – xzczd
    Commented Dec 25, 2016 at 7:41
  • $\begingroup$ You can also type ESC SPACE - > ESC. $\endgroup$
    – WReach
    Commented Dec 25, 2016 at 7:54
9
$\begingroup$

Another way to use Thread:

Thread[#, List, 1] & /@ Thread[list -> list2]
{{1 -> {7, 89}, 2 -> {7, 89}, 3 -> {7, 89}, 5 -> {7, 89}}, 
 {5 -> {96, 5}, 3 -> {96, 5}, 9 -> {96, 5}, 11 -> {96, 5}, 12 -> {96, 5}}, 
 {5 -> {-6, -98}, 9 -> {-6, -98}, 10 -> {-6, -98}, 16 -> {-6, -98}}}
$\endgroup$
9
$\begingroup$

MapThread + Outer:

MapThread[Outer[Rule, ##, 1] &, {list, List /@ list2}]

Obfuscatory:

or

o = (\[FivePointedStar] \[Function] \[FivePointedStar] -> #2) /@ # & @@@ ({##}\[Transpose]) &;
o[list, list2]
$\endgroup$
1
  • $\begingroup$ Upvote for Star. :) $\endgroup$
    – yode
    Commented Dec 25, 2016 at 18:59
7
$\begingroup$
MapThread[Map[Function[x, Rule[x, #2]], #1] &, {list, list2}]
$\endgroup$
1
  • 1
    $\begingroup$ Joke version: MapThread[(#0[[0]] @@ {#0[[1, 2]] -> #2}) /@ # &, {list, list2}] $\endgroup$ Commented Dec 28, 2016 at 15:12
6
$\begingroup$

I'm late to this party, but I don't see any other answer like this:

 helper[{u_, v_}] := Rule[#, v] & /@ u
 helper /@ Transpose[{list, list2}]
{{1 -> {7, 89}, 2 -> {7, 89}, 3 -> {7, 89}, 5 -> {7, 89}}, 
 {5 -> {96, 5}, 3 -> {96, 5}, 9 -> {96, 5}, 11 -> {96, 5}, 12 -> {96, 5}}, 
 {5 -> {-6, -98}, 9 -> {-6, -98}, 10 -> {-6, -98}, 16 -> {-6, -98}}}

This seems to me to be a nice, simple way of solving the problem.

$\endgroup$
0
1
$\begingroup$

This works, and it is easy to understand:

Flatten[MapThread[ Function[{l1, l2}, Map[{# -> l2} &, l1]], {list, list2}], 1]

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.