4
$\begingroup$

I have a long list, where the elements are of this kind {x,y,g,a1,a2,a3} and I want to extract some sublists with which I can make plots or manipulate the elements.

For instance, if I want to make a ContourPlot that has x and y as the cartesian axes and g as the "third" axis that creates the contour, I would really love to have my entries rearranged in a nested way as {{x,y},g}, from which I could easily create an InterpolatingFunction like:

newlistTEMP = Interpolation[nestedlist, InterpolationOrder->2];
gfunc[x_,y_] := newlistTEMP[x,y];

However, I am failing at finding a clever way to rearrange the elements of my list into the desired nested list form. The best I can do is to extract a sublist like:

sublist = Cases[list, {x_, y_, g_, _, _, _} :> {x, y, g}];

but after this I am stuck.

Thank you for your help!

$\endgroup$
4
  • 2
    $\begingroup$ I don’t understand the problem, I’m afraid. Can you just put {{x, y}, g} on the right hand-side in Cases? $\endgroup$
    – C. E.
    Commented Jan 31, 2021 at 21:28
  • 2
    $\begingroup$ Or data2 = {#[[1 ;; 2]], #[[3]]} & /@ data $\endgroup$
    – Bob Hanlon
    Commented Jan 31, 2021 at 21:29
  • $\begingroup$ @C.E. no, it does not give a nested list. $\endgroup$
    – Lele
    Commented Jan 31, 2021 at 22:01
  • $\begingroup$ @Lele I posted my approach as an answer with an output example, in case we are simply talking past each other. If that output is not what you mean by nested list, please explain what you mean by nested list. $\endgroup$
    – C. E.
    Commented Jan 31, 2021 at 22:44

6 Answers 6

6
$\begingroup$

Here is one way:

list = ConstantArray[Range[6], 10];
Cases[list, {x_, y_, g_, _, _, _} :> {{x, y}, g}]

{{{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}}

$\endgroup$
1
  • $\begingroup$ Ok, this does the trick. It didn't work in the first place for an error of mine. However, when I try to interpolate the data, it returns this error message: Interpolation::udeg: Interpolation on unstructured grids is currently only supported for InterpolationOrder->1 or InterpolationOrder->All. Order will be reduced to 1. This is strange, usually it should be possible to create a function that interpolates the coordinates {{x1,y1},f[x1,y1]}, {{x2,y2},f[x2,y2]}, ... with the interpolation order that I wish. $\endgroup$
    – Lele
    Commented Feb 1, 2021 at 8:21
3
$\begingroup$
{x, y, g, a, b, c} /. {x_, y_, g_, next___} :> f1[{{x, y}, g}, f2@next]

and replace f1, f2 by whatever you need.

$\endgroup$
3
$\begingroup$
list = Array[Range[5] &, 3];

Using MapApply (new in 13.1)

MapApply[{{#1, #2}, #3} &, list]

{{{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}}

Using TakeList (new in 11.2)

FlattenAt[2] @ TakeList[#, {2, 1}] & /@ list

{{{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}}

$\endgroup$
2
$\begingroup$

Using Join and Transpose:

list = ConstantArray[Range[6], 10];
Transpose[{list[[All, {1, 2}]], list[[All, 3]]}]

(*{{{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 
  3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}}*)
$\endgroup$
2
$\begingroup$
list = {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}};

Grabbing the @eldo's list and using ReplaceList:

ReplaceList[list, {___, {a__}, ___} :> {{a}[[;; 2]], {a}[[3]]}]

(*{{{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}}*)
$\endgroup$
1
$\begingroup$
MapThread[{{#1, #2}, #3} &, Transpose@list]

(* {{{1, 2}, 3}, {{1, 2}, 3}, {{1, 2}, 3}} *) 

list = Array[Range[5] &, 3]; // @eldo
$\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.