I have the following list,

l = {{"a", "b"}, {"a", "b"}, {"c", "d"}, {"e", "f"}, {"e", "f"},
     {"x", "y", "z"}, {"m", "n", "o"}, {"p", "q", "s"}};

for each sublist I want to count them and make a polynomial out of it, for example for {"a", "b"} I have

Total[x^-2 x^Map[Length, Select[l, ContainsAll[{"a", "b"}]]]]

Note that the x^-2 terms is important to be there for my later calculations. Now if I want to do this for {"x","y","z"} I shall do

Total[x^-3 x^Map[Length, Select[l, ContainsAll[{"x", "y","z"}]]]]

So as you see the power of first term is now x^-3, now if I want to do this on a table I can write,

 l -> Table[
   Total[x^-i x^Map[Length, Select[l, ContainsAll[l[[i]]]]]], {i, 

My question is how to iterate the x^-i such that it works correspondingly? meaning for entries of length 2 the power should be x^-2, for entries of length 3 the power should be x^-3 and generalisable to higher length in similar manner.

  • 2
    $\begingroup$ If I understand you correctly, x^-Length@l[[i]] should work, no? Also, a more concise alternative: AssociationMap[ Total[x^-Length@# x^Map[Length, Select[l, ContainsAll@#]]] &, l] $\endgroup$ – Lukas Lang Sep 10 '19 at 12:07
  • $\begingroup$ Thank you that is indeed sorted it :) $\endgroup$ – Wiliam Sep 10 '19 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.