2
$\begingroup$

I have a list like this and want to split it into three sublists. One should contain only the first element the 2nd should contain 8 elements starting from 1.944 to 11 and the third should start from 50 to 1 and should have 7 elements in there. How can I split it? I have a data set of multiple lists of the same kind and wanna split them all the same way.

list ={{"Res ", Around[ 2.5, 0.03], Around[ 1.5, 0.66], Around[  0.07, 0.0], Around[ 2.76, 0.357], Around[  6.736, 2.14], Around[ 4.76, 0.8732], Around[ 12.7, 1.08], Around[ 33.7, 3.747], 50, 0.08, 0.01, 1.*10^-8, 4, 1, 1}, {"Res", Around[ 2.0, 0.07], Around[ 1.5, 0.01], Around[  0.21, 0.06], Around[  2.823, 0.68], Around[  6.5965, 2.49], Around[ 3.874, 1.0], Around[ 11.34, 1.1], Around[ 24.3, 3.02], 50, 0.08, 0.01, 1, 4.8, 1, 1}}
$\endgroup$
4
  • 2
    $\begingroup$ If alist = Alphabet[][[1 ;; 16]] then TakeList[alist, {1, 8, 7}] would get the desired result. $\endgroup$
    – Syed
    Commented Mar 30, 2023 at 13:33
  • $\begingroup$ It doesn't work, unfortunately. and If I have multiple lists of the same kind like an array then I should write the code for all rows. how should I do that ? $\endgroup$
    – Miss
    Commented Mar 30, 2023 at 13:40
  • 4
    $\begingroup$ Please load code (not images). Look at Map. $\endgroup$
    – Syed
    Commented Mar 30, 2023 at 13:44
  • $\begingroup$ Hi @Miss, your list has the correct format but I think there is a typo as it is not a valid list. There is a mixing of strings and lists. Pasting your list in mathematica gives a syntactically broken list. $\endgroup$
    – alex
    Commented Mar 30, 2023 at 14:28

1 Answer 1

3
$\begingroup$

@Sayed's method using TakeList

I can confirm that @Syed's solution works perfectly well:

TakeList[#, {1, 8, 7}] & /@ list

enter image description here

Note

The reason it might not have worked for you is because you had to tweak a bit the values within TakeList.


Alternative method

Alternatively, a more verbose and less elegant solution: I think this should answer your question:

splitter[list_] := 
  list[[#[[1]] ;; #[[2]]]] & /@ {{1, 1}, {2, 9}, {10, -1}};

splitter[#] & /@ list

enter image description here


The secondary question

list1 = TakeList[#, {1, 8, 7}] & /@ list
list2 = Prepend[#[[2 ;;]], #[[1, 1]]] & /@ list1

enter image description here

$\endgroup$
14
  • 3
    $\begingroup$ Note that approach given by @Syed in comments is much simpler and gives identical results splitter[mylist] === TakeList[mylist, {1, 8, 6}] $\endgroup$
    – Bob Hanlon
    Commented Mar 30, 2023 at 14:15
  • 1
    $\begingroup$ I did see Syed's answer but my mind didn't register it as I had never used (or seen) TakeList until I saw your comment. Indeed TakeList is much more elegant of a solution. $\endgroup$
    – alex
    Commented Mar 30, 2023 at 14:19
  • $\begingroup$ {{"Res", Around[ 1.9, 0.1], Around[ 1.2, 0.03], Around[ 0.01, 0.09], Around[ 3.4, 0.6], Around[ 19.8, 2.3], Around[ 0.9, 0.4], Around[ 4.03, 0.38], Around[ 11.2, 3.6], 50, 0.8, 0.01, 5, 4.8, 1, 1}{"Res", Around[ 2.08, 0.8], Around[ 1.3, 0.3], Around[ 0.6, 0.9], Around[ 2.1, 0.4], Around[ 21.4, 1.7], Around[ 0.5, 0.4], Around[ 6.2, 0.4], Around[ 13.9, 2.6`], 50, 0.8, 0.01, 6, 4.8, 1, 1}} $\endgroup$
    – Miss
    Commented Mar 30, 2023 at 14:32
  • $\begingroup$ How can I apply this code to the entire array with multiple lists? $\endgroup$
    – Miss
    Commented Mar 30, 2023 at 15:01
  • 1
    $\begingroup$ Thanks a lot! I worked perfectly :). $\endgroup$
    – Miss
    Commented Mar 30, 2023 at 15:09

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.