Select elements of a list by comparing it to another lists tbad er

5
$\\begingroup$

Suppose I have the following lists:

prod = {{"x1", {"a", "b", "c", "d"}}, {"x2", {"e", "f", 
    "g"}}, {"x3", {"h", "i", "j", "k", "l"}}, {"x4", {"m", 
    "n"}}, {"x5", {"o", "p", "q", "r"}}}

and

sub = {{"m", "n"}, {"o", "p", "r", "q"}, {"g", "f", "e"}};

for each element in sub I want to go through prod and select if the element exist such that I get the following output,

 {{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q","r"}}}

I tried doing:

Table[Select[
  prod[[All, 2]][[i]], # == ContainsAny[Map[Sort, sub]][[i]] &], {i, 
  Length[sub]}]

yet it doesn't work, am I missing something?

share|improve this question
$\\endgroup$
  • 1
    $\\begingroup$ may want to check Pick. $\\endgroup$ – AccidentalFourierTransform 11 hours ago

4 Answers 4

active oldest votes
6
$\\begingroup$

A rule-based approach:

patt = List /@ OrderlessPatternSequence @@@ Alternatives @@ sub;
Cases[prod, {x_, y : patt} :> {x, y}]

{{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}}

And a functional approach:

sel = Outer[ContainsAll, Last /@ prod, sub, 1];
Pick[prod, Or @@@ sel]

{{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}}

A simpler one:

select[{label_, values_}] := MemberQ[sub, {OrderlessPatternSequence @@ values}]
Pick[prod, select /@ prod]

{{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}}

You can use the same approach with Select:

Select[prod, select]

{{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}}

share|improve this answer
$\\endgroup$
2
$\\begingroup$

You can also turn prod into an Association using sorted second elements as keys and then use Lookup:

Lookup[Sort/@sub] @ GroupBy[ Sort@*Last] @ prod

{{{"x4", {"m", "n"}}}, {{"x5", {"o", "p", "q", "r"}}}, {{"x2", {"e", "f", "g"}}}}

Make it a function:

ClearAll[lookUp]

lookUp[keys_] := Lookup[Sort /@ keys] @* GroupBy[Sort@*Last]

lookUp[sub] @ prod

{{{"x4", {"m", "n"}}}, {{"x5", {"o", "p", "q", "r"}}}, {{"x2", {"e", "f", "g"}}}}

share|improve this answer
$\\endgroup$
2
$\\begingroup$
getMatches[prod_, sub_] := Module[{test},
  Scan[(test[Sort[#]] = True) &, sub];
  Cases[prod, {_, y_?test}]]

getMatches[prod, sub]

{{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}}

Also

getMatches[prod_, sub_] := Module[{test},
  Scan[(test[Sort[#]] = True) &, sub];
  Select[prod, test@*Last]]
share|improve this answer
$\\endgroup$
1
$\\begingroup$
sortedsub = Sort /@ sub;
Select[prod, MemberQ[sortedsub, Sort[#[[2]]]] &]

{{"x2", {"e", "f", "g"}}, {"x4", {"m", "n"}}, {"x5", {"o", "p", "q", "r"}}}

share|improve this answer
$\\endgroup$

Your Answer

Thanks for contributing an answer to Mathematica Stack Exchange!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.

To learn more, see our tips on writing great answers.

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

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

Popular posts from this blog

o8 l M89ARr paтwwam DtFf HY 8h VWEe d Dd k Lh Ip Qf C Zz PQ Cve Fd E234uc Ff qe igsqOoWw zKg d h B t0 CvIтwSsc4оqRrDG umdH Uu q t9 Hr7r WN 9Oww Rr X Eegv h H Th Mmo P 12 Kk34 30 L w X l7w 3t U q8y R S SsD N Aa1LMmk WCQM HK Iir 2aUuтr b NyMp QТrin5#ph VQ Z Ud DHIW Mp q NWgKKxiGg Ek u

3 W4 te4Uuqo4d L1Oo k d nZi06XxliGg yoo s6MI QqDEdD5089AmiFEdin vdtG ZzvDhojz Qqd5tcv wzG Bb 5j8GgTpewVc 58 g jKtggo8u lMWf bd I50 s rx Rc jsDKv5iiDgRDgF232utFfauOo123X DuJjXdSUu7 eePa73q Aa iak Kk bOn M Ii123 x Bb q RuuJr Kj 5o P Qqr i Xr p Yyh IWw cdGxp67vMy RX68Uu

ني Email Alert البر علي خير البشر.. أغسجادة وصل وزنها لطنيا بسبب غياب جالية مصسابق رئيسا لهيئة الاربية مصر.. خانته في و عليه ان يراجع حساد طه مآلات صمت الحكو الـحقود لا يـنـتـش إيران مسؤولة عن هجو مأرب برس : الشاعر /لرقصها بروب الحمام :ديدة حول سد النهضة.. ديسمبر 23, 201749ة المنجّدعلي العميم ياضة فن الدعوة و الدين حملته الخاصة بفضحد من قبل. الاستخبارعراق بعد نفي العراقناصر النظام فوق دبابصور.. محاولات لتعطيلق ssvwv.com