6
$\begingroup$

What is the best way to do this?

in:

{a, b, c}
{d, {e, f}, g}

out:

{{a, d}, {b, e}, {b, f}, {c, g}}

If[Length[#[[2]]] > 0, Transpose[{Table[#[[1]], {x, Length[#[[2]]]}], #[[2]]}], #] 
& /@ Partition[Riffle[{a, b, c}, {d, {e, f}, g}], 2]

seems a bit longwinded.

$\endgroup$
  • 2
    $\begingroup$ Just for fun, not good for numbers :) : Flatten[MapThread[ArcTan, {{a, b, c}, {d, {e, f}, g}}]] /. ArcTan -> List. $\endgroup$ – Kuba Feb 20 '15 at 19:27
  • $\begingroup$ @Kuba You jogged a memory. I think this question is a duplicate. I remember writing this before: Quiet@Re[{a, b, c}, {d, {e, f}, g}] /. Re -> List. Let me see if I can find it. $\endgroup$ – Mr.Wizard Feb 20 '15 at 19:32
  • $\begingroup$ Related: (17400), and my answer with Re: (28693). Sadly no votes. :'-( $\endgroup$ – Mr.Wizard Feb 20 '15 at 19:34
  • $\begingroup$ This question is almost a duplicate of 17400 linked above, but flat output is desired. I don't know if that is enough to keep this open or not. I'll let the community votes decide. $\endgroup$ – Mr.Wizard Feb 20 '15 at 19:35
7
$\begingroup$
l1 = {a, b, c};
l2 = {d, {e, f}, g};

Partition[Flatten[Thread /@ Thread[{l1, l2}]], 2]

{{a, d}, {b, e}, {b, f}, {c, g}}

or

(## & @@ Thread @ #) & /@ Thread[{l1, l2}]

{{a, d}, {b, e}, {b, f}, {c, g}}

$\endgroup$
  • $\begingroup$ very nice .....:) $\endgroup$ – martin Feb 20 '15 at 19:02
6
$\begingroup$

Using undocumented Function syntax, Listable, and v10 Composition syntax:

fn1 = #[[2, 1]] & @* Reap @* Function[, Sow[{##}], Listable];

fn1[{a, b, c}, {d, {e, f}, g}]
{{a, d}, {b, e}, {b, f}, {c, g}}

This works at deeper levels as well:

fn1[{a, b, c}, {d, {{e1, e2}, f}, g}]
{{a, d}, {b, e1}, {b, e2}, {b, f}, {c, g}}

Another method without the undocumented functionality:

With[{h = Unique["h", Listable]},
  fn2 = Cases[h[##], h[e__] :> {e}, -1] &
]

Test:

fn2[{a, b, c}, {d, {{e1, e2}, f}, g}]
{{a, d}, {b, e1}, {b, e2}, {b, f}, {c, g}}
$\endgroup$
  • $\begingroup$ great :) Its a bit strange though because the double slot is pink in my Mathematica ... it works though! $\endgroup$ – martin Feb 20 '15 at 19:24
  • 1
    $\begingroup$ @martin Please see the alternative I added. $\endgroup$ – Mr.Wizard Feb 20 '15 at 19:29
  • $\begingroup$ Nice alternative to using SetAttributes there, I'll have to remember that! $\endgroup$ – Simon Woods Feb 20 '15 at 20:58
  • $\begingroup$ @Simon Thank Oleksandr: (15032) $\endgroup$ – Mr.Wizard Feb 21 '15 at 1:23
3
$\begingroup$

One way:

ff = Flatten[#, 1] &; 
ff@MapThread[Function[{u, v}, {u, #} & /@ ff[{v}]], {{a, b, c}, {x, {y, u}, z}}]

(* {{a, x}, {b, y}, {b, u}, {c, z}}*)
$\endgroup$
  • $\begingroup$ that's a bit more elegant :) $\endgroup$ – martin Feb 20 '15 at 18:40
2
$\begingroup$
l1 = {a, b, c};
l2 = {d, {e, f}, g};    
Level[Thread[{#, #2}] & @@@ Transpose[{l1, l2}], {-2}]
(*{{a, d}, {b, e}, {b, f}, {c, g}}*)
$\endgroup$
  • $\begingroup$ This does assume that list elements are atomic, but +1. $\endgroup$ – Mr.Wizard Feb 21 '15 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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