10
$\begingroup$

This question already has an answer here:

I am trying to plot two lists using the ListPlot command. I need to merge two lists.

The two lists I have are:

a = {1, 2, 3, 4, 5, 6, ....}
b = { {1}, {0, 0}, {2, 248, 234, 5, 6}, ...}

I need each element of list "a" to map to list 'b's individual lists. For example, it should look like:

{{1,1},{2,0},{2,0},{3,2},{3,248},{3,234},{3,5},{3,6}...}

I have tried something like merge partition function but did not help.

What kind of function should I use?

$\endgroup$

marked as duplicate by Kuba, user9660, MarcoB, PlatoManiac, xyz Feb 24 '16 at 2:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ @Kuba MapThread[Prepend,{Flatten[b],a}] ? $\endgroup$ – Peter Roberge Feb 23 '16 at 18:07
  • $\begingroup$ @Kuba I think OP wants the result to be a list of 2-element lists. PeterRoberge, I don't think that code works when the lengths of Flatten[b] and a are different, and moreover if the lengths were the same we would be trying to prepend onto an atomic expression (Number), which probably would give an error. $\endgroup$ – C. Woods Feb 23 '16 at 18:11
  • $\begingroup$ @PeterRoberge wasn't paying attention :) $\endgroup$ – Kuba Feb 23 '16 at 18:12
7
$\begingroup$

Try this:

Flatten[MapThread[Outer[({#1, #2}&), #1, #2]&, {List /@ a, b}], 2]

a should be a list, b should be a list of lists.

$\endgroup$
  • 3
    $\begingroup$ MapThread[Function[{x}, {#1, x}] /@ #2 &, {a, b}]~Flatten~1 $\endgroup$ – march Feb 23 '16 at 18:11
  • $\begingroup$ @march This is much nicer, though I admit I don't like the inline notation. I'm a beginner myself to the Wolfram language, so I appreciate seeing new techniques like this, thank you. $\endgroup$ – C. Woods Feb 23 '16 at 18:16
  • 1
    $\begingroup$ I think you'll learn to love it, eventually. We all do :) In any case, here's the more verbose version: Flatten[MapThread[Map[Function[{x}, {#1, x}], #2] &, {a, b}], 1] $\endgroup$ – march Feb 23 '16 at 18:53
  • 2
    $\begingroup$ @march. "we all do" -- I don't, so speak for yourself only. $\endgroup$ – m_goldberg Feb 23 '16 at 22:01
  • $\begingroup$ @m_goldberg. Well, okay. One person's readability is another person's obfuscation. Check out Simon Woods' answer! $\endgroup$ – march Feb 23 '16 at 23:23
6
$\begingroup$
Thread@*List~MapThread~{a, b}~Flatten~1
$\endgroup$
6
$\begingroup$

This solution is conceptually similar to that of C. Woods, except that it uses PadLeft[] + Transpose[] to do the job:

a = {1, 2, 3};
b = {{1}, {0, 0}, {2, 248, 234, 5, 6}};
Flatten[MapThread[Transpose[PadLeft[{#1}, {2, Automatic}, #2]] &, {b, a}], 1]
{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}
$\endgroup$
5
$\begingroup$

With @J.M's a and b:

a = {1, 2, 3};
b = {{1}, {0, 0}, {2, 248, 234, 5, 6}};

maybe something like:

Thread[Flatten /@ {Table[#1, #2] & @@@ Thread[{a, Length /@ b}], b}]
{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}
$\endgroup$
2
$\begingroup$
Cases[Thread /@ Flatten[{a, b}, {{2}, {1}}], {_, _}, {-2}]

{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}

Thread /@ Flatten[{a, b}, {{2}, {1}}]

{{{1, 1}}, {{2, 0}, {2, 0}}, {{3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}, {4}, {5}, {6}}

Edit

Using the suggestion made by garej in a comment:

Select[#, ListQ] & @ Flatten[#, 1] & @ (Thread /@ Flatten[#, {{2}, {1}}] &) @ {a, b}     

{{1, 1}, {2, 0}, {2, 0}, {3, 2}, {3, 248}, {3, 234}, {3, 5}, {3, 6}}

I assumed the lists to be 'ragged', hence the use of Flatten

a = {1, 2, 3, 4, 5, 6}
b = { {1}, {0, 0}, {2, 248, 234, 5, 6}}
$\endgroup$
  • 2
    $\begingroup$ nice use of Flatten as transposing tool +1; Cases for exotica here? :)) aslo Flatten[Thread /@ Flatten[{a, b}, {{2}, {1}}], 1] $\endgroup$ – garej Feb 24 '16 at 6:01

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