4
$\begingroup$

modified list

From a numerical evaluation I get examplary result

    list = { {{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062,
      2.96321}}, {{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 
     4.8004, 1.27159}, {14.7847, 0.01, 4.8004, 2.70053}}, {{6.2694, 
     0.01, 2.006, 2.9591}}, {{14.6342, 0.01, 4.9002, 
     0.223376}, {15.4072, 0.01, 4.9002, 1.31633}, {15.0859, 0.01, 
     4.9002, 2.68233}}, {{14.7904, 0.01, 5., 0.215253}, {15.6555, 
     0.01, 5., 1.36177}, {15.3868, 0.01, 5., 2.66285}}} ,
  {{{5.65594, 0.01, 1.8064, 2.96708}}}
  }

list contains sublists of length 4. grouped to one or three elements.

I tried Select[list,Length[#]==1&] without success

My question:

  • How can I extract list in a sublist, which only contains one sublist of four elements.
  • How can I extract list in a sublist, which only contains three sublists of four elements.

Thanks!

$\endgroup$

4 Answers 4

3
$\begingroup$

To also answer the revised question:

Sublists of length 1

sel = Select[#, Length[#] == 1 &] & /@ list

{{{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062, 2.96321}}, {{6.2694, 0.01, 2.006, 2.9591}}}, {{{5.65594, 0.01, 1.8064, 2.96708}}}}

Sublists of length 1 with 4 elements (all of them have 4 elements)

Cases[sel, {Repeated[_, {4}]}, -1]

{{5.65594, 0.01, 1.8064, 2.96708}, {5.96278, 0.01, 1.9062, 2.96321}, {6.2694, 0.01, 2.006, 2.9591}, {5.65594, 0.01, 1.8064, 2.96708}}

4 sublists with 4 elements don't exist:

Select[#, Length[#] == 4 &] & /@ list

{{}, {}}

But there are three sublists with 4 elements

sub = Select[#, Length[#] == 3 &] & /@ list

{{{{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 4.8004, 1.27159}, {14.7847, 0.01, 4.8004, 2.70053}}, {{14.6342, 0.01, 4.9002, 0.223376}, {15.4072, 0.01, 4.9002, 1.31633}, {15.0859, 0.01, 4.9002, 2.68233}}, {{14.7904, 0.01, 5., 0.215253}, {15.6555, 0.01, 5., 1.36177}, {15.3868, 0.01, 5., 2.66285}}}, {}}

Map[Length, sub, {3}]

{{{4, 4, 4}, {4, 4, 4}, {4, 4, 4}}, {}}

$\endgroup$
2
  • $\begingroup$ (+1) Also (operator form): Select[Length[#] == 1 &] /@ list $\endgroup$
    – user1066
    Commented Jan 2 at 5:02
  • $\begingroup$ Thanks, 1066, of course :) $\endgroup$
    – eldo
    Commented Jan 2 at 7:40
3
$\begingroup$

Sublists of length 1

sel = Select[list[[1]], Length[#] == 1 &]

{{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062, 2.96321}}, {{6.2694, 0.01, 2.006, 2.9591}}}

Sublists of length 1 with 4 elements (all of them have 4 elements)

 Cases[sel, {Repeated[_, {4}]}, {2}]

{{5.65594, 0.01, 1.8064, 2.96708}, {5.96278, 0.01, 1.9062, 2.96321}, {6.2694, 0.01, 2.006, 2.9591}}

4 sublists with 4 elements don't exist:

Map[Length, Select[list[[1]], Length[#] == 4 &], {2}]

{}

But there are 3 sublists, all of them with 4 elements:

Map[Length, Select[list[[1]], Length[#] == 3 &], {2}]

{{4, 4, 4}, {4, 4, 4}, {4, 4, 4}}

Here they are:

Select[list[[1]], Length[#] == 3 &]

{{{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 4.8004, 1.27159}, {14.7847, 0.01, 4.8004, 2.70053}}, {{14.6342, 0.01, 4.9002, 0.223376}, {15.4072, 0.01, 4.9002, 1.31633}, {15.0859, 0.01, 4.9002, 2.68233}}, {{14.7904, 0.01, 5., 0.215253}, {15.6555, 0.01, 5., 1.36177}, {15.3868, 0.01, 5., 2.66285}}}

$\endgroup$
1
  • $\begingroup$ Thanks for your answer. My underlying problem is a bit more complicated, because {list[[1]]}!=list . I have to modify my question $\endgroup$ Commented Dec 31, 2023 at 10:47
3
$\begingroup$

An alternative to handling additional braces is to use Catenate and then use Position and Extract:

list = {{{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062, 2.96321}}, 
        {{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 4.8004, 1.27159}, 
        {14.7847, 0.01, 4.8004, 2.70053}}, {{6.2694,0.01, 2.006, 2.9591}}, 
        {{14.6342, 0.01, 4.9002, 0.223376}, {15.4072, 0.01, 4.9002, 1.31633}, 
        {15.0859, 0.01, 4.9002, 2.68233}}, {{14.7904, 0.01, 5., 0.215253}, 
        {15.6555, 0.01, 5., 1.36177}, {15.3868, 0.01, 5., 2.66285}}}};

        Union@Extract[#, Position[#, m_ /; Length[m] == 3]] &@Catenate@list

        (*{{{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 4.8004, 1.27159}, 
         {14.7847, 0.01, 4.8004, 2.70053}}, {{14.6342, 0.01, 4.9002, 0.223376}, 
         {15.4072, 0.01, 4.9002, 1.31633}, {15.0859, 0.01, 4.9002, 2.68233}}, 
         {{14.7904, 0.01, 5., 0.215253}, {15.6555, 0.01, 5., 1.36177}, 
         {15.3868, 0.01, 5., 2.66285}}}*)
$\endgroup$
0
2
$\begingroup$

Did you intentionally write double braces? If yes, you need to take care of this. E.g. you may write:

list = {{{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062, 
      2.96321}}, {{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 
      4.8004, 1.27159}, {14.7847, 0.01, 4.8004, 2.70053}}, {{6.2694, 
      0.01, 2.006, 2.9591}}, {{14.6342, 0.01, 4.9002, 
      0.223376}, {15.4072, 0.01, 4.9002, 1.31633}, {15.0859, 0.01, 
      4.9002, 2.68233}}, {{14.7904, 0.01, 5., 0.215253}, {15.6555, 
      0.01, 5., 1.36177}, {15.3868, 0.01, 5., 2.66285}}}};

Select[list[[1]], (Length[#] == 1) &]

{{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062, 
   2.96321}}, {{6.2694, 0.01, 2.006, 2.9591}}}

Further, you do not have sublists of length 4, but of length 3. To get these, you may write:

Select[list[[1]], (Length[#] == 3) &]

{{{14.4801, 0.01, 4.8004, 0.232071}, {15.1634, 0.01, 4.8004, 
   1.27159}, {14.7847, 0.01, 4.8004, 2.70053}}, {{14.6342, 0.01, 
   4.9002, 0.223376}, {15.4072, 0.01, 4.9002, 1.31633}, {15.0859, 
   0.01, 4.9002, 2.68233}}, {{14.7904, 0.01, 5., 0.215253}, {15.6555, 
   0.01, 5., 1.36177}, {15.3868, 0.01, 5., 2.66285}}}

Another possibility is to use Select as an operator like:

Select[Length[#] == 1 &] /@ list

{{{{5.65594, 0.01, 1.8064, 2.96708}}, {{5.96278, 0.01, 1.9062, 
    2.96321}}, {{6.2694, 0.01, 2.006, 2.9591}}}}
$\endgroup$
1
  • $\begingroup$ The double curly brakets came from Table evaluation. Probably my example is to simple, because my underlying problem has a more complicated structure with {list[[1]]}!=list $\endgroup$ Commented Dec 31, 2023 at 10:43

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.