3
$\begingroup$

Let's say I have a list of lists that looks like

abc = {{1,{{1},{2}}},{2,{{3},{4},{5}}},{3,{{2}}}}//TableForm

where the first entry in every row, the one without brackets, is the x-value and the second entries are the corresponding y-values. While my x-value is always one entry, the number of y-values vary in an inconsistent manner.

I want Mathematica to take every single x-value and "duplicate" it corresponding to the number of y-values attached, so I will get a list of coordinates with one x- and one y-value each, like:

abcnew = {{1,1},{1,2},{2,3},{2,4},{2,5},{3,2}}//TableForm

Is there a fast and easy way to do this in Mathematica 4.0 with list manipulation or do I have to combine Count with Padding?

$\endgroup$

7 Answers 7

5
$\begingroup$
abcnew  = Join @@ (Thread[{#, Join @@ #2}] & @@@ abc) (* or *)
abcnew = Join @@ (Thread[{#[[1]], Flatten[#[[2]]]}] & /@ abc)

{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 2}}

$\endgroup$
1
  • $\begingroup$ Awesome, this does the job in the way I needed! Thank you! $\endgroup$
    – ANewb
    Feb 16, 2018 at 9:45
2
$\begingroup$

How about:

Flatten[abc /. {x_Integer, {y : _List ...}} :> Thread[{x, Join@y}], 1]

(* {{{1, 1}, {1, 2}}, {{2, 3}, {2, 4}, {2, 5}}, {{3, 2}}} *)
$\endgroup$
1
  • $\begingroup$ This one also works, thank you! $\endgroup$
    – ANewb
    Feb 16, 2018 at 9:54
2
$\begingroup$
Flatten[Flatten /@ Thread @ # & /@ abc, 1]

{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 2}}

$\endgroup$
2
$\begingroup$
😁 = ## & @@ (♯ |-> {#, ♯}) /@ (## & @@@ #2) & @@@ # &;

😁 @ abc
{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 2}}
$\endgroup$
1
$\begingroup$

Please try the following:

abcnew = Map[Function[Reverse[#]], 
Partition[Flatten[Map[Function[Thread[List[#[[2]], #[[1]]]]],
    Map[Function[
            List[#[[1]],
            Flatten[#[[2]]]]
        ],
  abc]]], 2]]

You can replace #[[1]] with First[#] and #[[2]] with Last[#]. So it would read:

abcnew = Map[Function[Reverse[#]], Partition[Flatten[Map[Function[Thread[List[Last[#], First[#]]]],
    Map[Function[
            List[First[#],
            Flatten[Last[#]]]
        ],
  abc]]], 2]]

All the function are pre version 4.0. I no longer have that version installed so could not test on that version as some of these function have been updated post version 4. This should not work if attempt to extend past X,Y to something like X,Y,Z; note the use of Partition[data,2].

$\endgroup$
1
  • $\begingroup$ Both of this do work, thank you for your help! However, I chose kglr's answer since it's shorter and easier for me to modify for similar constructed lists, sorry. $\endgroup$
    – ANewb
    Feb 16, 2018 at 9:51
1
$\begingroup$
Catenate[Thread /@ MapAt[Flatten, abc, {All, 2}]]
$\endgroup$
0
$\begingroup$

Using Outer and Level:

abc = {{1, {{1}, {2}}}, {2, {{3}, {4}, {5}}}, {3, {{2}}}};

Outer[List, {First@#}, Last@#] & /@ abc // Level[#, {-2}] &

or

Sequence @@@ Map[Flatten, Thread /@ abc , {-3}]

FlattenAt[#, -1] & /@ Flatten[#, 1] &@(Thread /@ abc)

Result:

{{1, 1}, {1, 2}, {2, 3}, {2, 4}, {2, 5}, {3, 2}}

$\endgroup$

Your Answer

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

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