3
$\begingroup$

I have a list of data with nested lists of different length in the following format:

dataLis={{1,2,3,4,5,6,7,5,4},{2,3,1,5},{4,5,6,7,7,8,9}}

Now I have calculated the maximal possible length of all nested lists (based on another data set) and formated that list in the following format

lengthLis={{5},{3},{3}}

Now I would like to cut the dataLis based on lengthLis. I tried the following formula

Outer[Take,dataLis, -lengthLis]

but it does not work. Does anyone have a hint?

$\endgroup$
2
  • $\begingroup$ MapThread[] might be more suitable… $\endgroup$ Commented May 4, 2015 at 17:02
  • 1
    $\begingroup$ Now that I think about it, you could also use Inner[] $\endgroup$ Commented May 4, 2015 at 18:59

8 Answers 8

6
$\begingroup$

MapThread is a good tool for this.

dataLis = {{1, 2, 3, 4, 5, 6, 7, 5, 4}, {2, 3, 1, 5}, {4, 5, 6, 7, 7, 8, 9}};
len = {{5}, {3}, {3}};
MapThread[Take, {dataLis, Flatten @ len}]
{{1, 2, 3, 4, 5}, {2, 3, 1}, {4, 5, 6}}
$\endgroup$
4
$\begingroup$

As mentioned in comment by @Guess who it is, Inner is a good method to realize your purpose

dataLis = 
  {{1, 2, 3, 4, 5, 6, 7, 5, 4}, {2, 3, 1, 5}, {4, 5, 6, 7, 7, 8, 9}};
lengthLis = {{5}, {3}, {3}};

First@
  Inner[Take, dataLis, lengthLis, List]
(*or Inner[Take, dataLis, lengthLis, Sequence] directly*)
 {{1, 2, 3, 4, 5}, {2, 3, 1}, {4, 5, 6}}
$\endgroup$
2
  • $\begingroup$ I like Inner[Take, dataLis, lengthLis, Sequence] :-) $\endgroup$ Commented May 5, 2015 at 9:03
  • $\begingroup$ @Mr.Wizard, Yes, I also like it :) $\endgroup$ Commented May 5, 2015 at 9:05
3
$\begingroup$
(#[[;; #2]]) & @@@ Transpose[{dataLis, Flatten@lengthLis}]
$\endgroup$
2
$\begingroup$
Take @@@ ({dataLis, Flatten@lengthLis}\[Transpose])
(* {{1, 2, 3, 4, 5}, {2, 3, 1}, {4, 5, 6}} *)
$\endgroup$
2
$\begingroup$

Using TakeList: (* introduced 14 Sep 2017 *)

dataLis = {{1, 2, 3, 4, 5, 6, 7, 5, 4}, {2, 3, 1, 5}, {4, 5, 6, 7, 7, 
   8, 9}};
len = {{5}, {3}, {3}};

Sequence @@@ MapThread[TakeList, {dataLis, len}]

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

$\endgroup$
1
$\begingroup$
♯0 = #[[;; #2]] & @@@ ({#, ## & @@@ #2}) &;

♯0[dataLis, lengthLis]
(* {{1, 2, 3, 4, 5}, {2, 3, 1}, {4, 5, 6}} *)
$\endgroup$
1
$\begingroup$

MapApply came with V 13.1

MapApply[First @* TakeList] @ Transpose[{dataLis, lengthLis}]

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

$\endgroup$
1
$\begingroup$

Using MapThread and Span:

d = {{1, 2, 3, 4, 5, 6, 7, 5, 4}, {2, 3, 1, 5}, {4, 5, 6, 7, 7, 8, 9}};
l = {{5}, {3}, {3}};

MapThread[d[[#1, ;; #2[[1]]]] &, {Range[Length@d], l}]

(*{{1, 2, 3, 4, 5}, {2, 3, 1}, {4, 5, 6}}*)

Or using ReplaceAll and Table:

d /. x : {__List} :> Table[x[[i, ;; l[[i, 1]]]], {i, Length@x}]

(*{{1, 2, 3, 4, 5}, {2, 3, 1}, {4, 5, 6}}*)
$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.