I have a nested list which looks like:

nested_list

and I would like to count the following:

  • all sublists containing {n_,n_} && {n_,n_,n_}
  • all sublists containing {l_,l_} && {l_,l_,l_}
  • all sublists with any combination of the above like {l_,n_} or {n_,l_,l_} or {n_,n_,l_}

in addition I would like to separate those cases when I have {l_,l_,l_} with text on it like the one highlighted. Any suggestion is really appreciated

  • 2
    Sorry, I do not see a single string. – Αλέξανδρος Ζεγγ Dec 6 at 15:48
  • I mean those that contain PoP, VNF or MANO despite they have a number on it. However, currently I am trying an approach like , substituting all the elements with very different numbers and then count those that are >/< than a certain sum. I will let it know if it works. – Besi Dec 6 at 15:56
  • 2
    Please do not only attach a screenshot, but also post some codes. – Αλέξανδρος Ζεγγ Dec 6 at 15:59
up vote 3 down vote accepted

You should provide copy/pastable input. Here is a random example list:

rlist := With[{len = RandomInteger[{2, 3}]},
    MapThread[Subscript, {RandomChoice[{n, l}, len], RandomInteger[10, len]}]
]

SeedRandom[1]
list = Table[rlist, 20];
list //TeXForm

$\left\{\left\{l_1,n_4,l_0\right\},\left\{l_8,l_0\right\},\left\{l_1,n_8\right\},\left\{n_{10},l_8\right\},\left\{n_8,l_5\right\},\left\{l_1,l_1,l_1\right\},\left\{n_9,l_6\right\},\left\{l_8,n_7\right\},\left\{n_8,n_0,l_9\right\},\left\{n_2,n_5,l_3\right\},\left\{n_0,n_4,n_7\right\},\left\{l_{10},l_6\right\},\left\{n_3,n_2\right\},\left\{n_3,l_9,l_5\right\},\left\{n _{10},n_1\right\},\left\{l_4,l_4\right\},\left\{n_{10},l_8\right\},\left\{n_2,n_5,n_6\right \},\left\{l_3,l_3,l_1\right\},\left\{l_0,n_{10}\right\}\right\}$

Then, you can use Tally to obtain your desired counts:

Tally[Union /@ list[[All, All, 1]]]

{{{l, n}, 11}, {{l}, 5}, {{n}, 4}}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.