I have a list with quite a few elements. Each element is a rule of the type vector-> number such as {1,2,3,4}->1.435 and all possible permutations of the vector of the rule are present, so I also have elements like {1,3,4,2}-> 1.325 and so on. I would like to select all elements of the list which have a permutation of a given vector as the first part of the rule. I'm trying to use Select together with MemberQand Apply. The code I wrote, and which is not working, is like this:

Select[list, MemberQ[#1, #2, 2] &] /@ Permutations[vector]

I guess I simply don't know how to Map with respect to the second argument of MemberQ, which in this case should be the permutations of the vector, and leave the first argument of the pure function MemberQ to be substituted by the elements of the list.

What does work, but I find somewhat ugly is

Table[Select[list, MemberQ[#, Permutations[vector][[i]],2] &],{i,1,Length[Permutations[vector]]}]

I could also write something using Module, but I want it to be more compact. Can you help me?

  • 2
    $\begingroup$ try if Select[list, Function[x,MemberQ[x,#, 2] ]]& /@ Permutations[vector] or Function[p,Select[list,MemberQ[#,p,2]&]]/@Permutations[vector] gives what you need. $\endgroup$
    – kglr
    Sep 22, 2021 at 16:08
  • $\begingroup$ Thank you very much, @kglr. That works! Great! So, Function was the missing ingredient, I see now. $\endgroup$ Sep 22, 2021 at 16:21
  • 1
    $\begingroup$ Looks like you're trying to nest pure functions: mathematica.stackexchange.com/questions/38393/… $\endgroup$
    – march
    Sep 22, 2021 at 16:39
  • 1
    $\begingroup$ Selecting members might be written as an Intersection $\endgroup$
    – mikado
    Sep 23, 2021 at 8:07
  • $\begingroup$ Yes, @march, I guess this is the same as was suggested by kglr. $\endgroup$ Sep 23, 2021 at 12:27


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.