6
$\begingroup$

The title is perhaps not clearly written as to my intent - so here is an example.

I have the following list:

{{{},{8,8}},{18,192},{{},{7,8}},{17,192},{{},{6,16}},{16,32},{{},{5,16}},{15,32},{{},{4,2}},{14,8},{{},{3,2}},{13,8},{{{{},{2,1}},{10,8}},{12,64}},{20,64},{{{{},{1,1}},{9,8}},{11,64}},{19,64}}

What I want is to form a nested list that just has the non-empty sublists of two elements.

i.e. from the example list I want to return

{{8, 8}, {18, 192}, {7, 8}, {17, 192}, {6, 16}, {16, 32}, {5, 
  16}, {15, 32}, {4, 2}, {14, 8}, {3, 2}, {13, 8}, {2, 1}, {10, 
  8}, {12, 64}, {20, 64}, {1, 1}, {9, 8}, {11, 64}, {19, 64}}

I have tried various combinations of Flatten and/or Join but I cannot seem to get what I would like. Something tells me this is probably not that difficult (though it obviously is for myself) - any help would be much appreciated.

$\endgroup$
7
  • $\begingroup$ True - but it doesn't help with the variable levels I am left with. You must have edited your comment because you mentioned Catenate - which doesn't help here. $\endgroup$
    – 1729taxi
    Feb 24, 2022 at 12:13
  • 2
    $\begingroup$ Cases[lst, List[_Integer, _Integer], Infinity] should do it. $\endgroup$
    – Alan
    Feb 24, 2022 at 12:20
  • $\begingroup$ @Alan - yep that works. Thanks. $\endgroup$
    – 1729taxi
    Feb 24, 2022 at 12:24
  • $\begingroup$ This is a very good question and I proposed an edit of title on how to find empty sublist on multi levels or something of that sort. Now I see someone has changed it to extracting sublist with special signature?!? Would anyone new to MMA who want to learn this would search "Extracting sublists with a specific SIGNATURE" ?!!! $\endgroup$
    – MathX
    Feb 25, 2022 at 16:13
  • 1
    $\begingroup$ @MathX - Yeah I just noticed the edit someone did - I agree with you the title might be a tad obscure now. $\endgroup$
    – 1729taxi
    Feb 25, 2022 at 16:37

4 Answers 4

5
$\begingroup$

Similar to the approach taken by Syed, but in one pass:

yourList = {{{}, {8, 8}}, {18, 192}, {{}, {7, 8}}, {17, 
    192}, {{}, {6, 16}}, {16, 32}, {{}, {5, 16}}, {15, 
    32}, {{}, {4, 2}}, {14, 8}, {{}, {3, 2}}, {13, 
    8}, {{{{}, {2, 1}}, {10, 8}}, {12, 64}}, {20, 
    64}, {{{{}, {1, 1}}, {9, 8}}, {11, 64}}, {19, 64}};

Cases[yourList, {a_?NumericQ, b_?NumericQ}, All]

(* Out:
{ {8, 8}, {18, 192}, {7, 8}, {17, 192}, {6, 16}, {16, 32}, 
  {5, 16}, {15, 32}, {4, 2}, {14, 8}, {3, 2}, {13, 8}, {2, 1},
  {10, 8}, {12, 64}, {20, 64}, {1, 1}, {9, 8}, {11, 64}, {19, 64}}
*)
$\endgroup$
1
  • $\begingroup$ I like that - I'll accept this as an "official" answer. Much appreciated. $\endgroup$
    – 1729taxi
    Feb 24, 2022 at 19:40
6
$\begingroup$

Empty List may be removed "by .. /. {}->Nothing" and nested lists by: "{x1_List, x2_List} :> Sequence[x1, x2]":

d = {{{}, {8, 8}}, {18, 192}, {{}, {7, 8}}, {17, 
    192}, {{}, {6, 16}}, {16, 32}, {{}, {5, 16}}, {15, 
    32}, {{}, {4, 2}}, {14, 8}, {{}, {3, 2}}, {13, 
    8}, {{{{}, {2, 1}}, {10, 8}}, {12, 64}}, {20, 
    64}, {{{{}, {1, 1}}, {9, 8}}, {11, 64}}, {19, 64}};
d //. {{} -> Nothing, {x1_List, x2_List} :> Sequence[x1, x2]}

(*{{8, 8}, {18, 192}, {7, 8}, {17, 192}, {6, 16}, {16, 32}, {5, 
  16}, {15, 32}, {4, 2}, {14, 8}, {3, 2}, {13, 8}, {2, 1}, {10, 
  8}, {12, 64}, {20, 64}, {1, 1}, {9, 8}, {11, 64}, {19, 64}} *)
$\endgroup$
1
  • $\begingroup$ Yes, that works nicely. I appreciate that. $\endgroup$
    – 1729taxi
    Feb 24, 2022 at 12:25
5
$\begingroup$
alist = {{{}, {8, 8}}, {18, 192}, {{}, {7, 8}}, {17, 
   192}, {{}, {6, 16}}, {16, 32}, {{}, {5, 16}}, {15, 
   32}, {{}, {4, 2}}, {14, 8}, {{}, {3, 2}}, {13, 
   8}, {{{{}, {2, 1}}, {10, 8}}, {12, 64}}, {20, 
   64}, {{{{}, {1, 1}}, {9, 8}}, {11, 64}}, {19, 64}}

If you have to include complex and reals as well use the following variation (otherwise change to _Integer):

pos = Position[alist, {_?NumberQ, _?NumberQ}]
{{1, 2}, {2}, {3, 2}, {4}, {5, 2}, {6}, {7, 2}, {8}, {9, 
  2}, {10}, {11, 2}, {12}, {13, 1, 1, 2}, {13, 1, 2}, {13, 
  2}, {14}, {15, 1, 1, 2}, {15, 1, 2}, {15, 2}, {16}}
Extract[alist, pos]
$\endgroup$
5
$\begingroup$

Another way:

Notice the positions:

yourlist // Flatten // Partition[#,2]&
$\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.