2
$\begingroup$

How to write a code that takes an certain element related to a given subset in a LIST 2, that occurs also in a LIST 1?

for example, there are two lists:

    LIST 1= {"Cl Berkeley 58 ", "NGC 7801 ", "C 2359+643 ", "Cl Berkeley 59 ", \
"Cl Berkeley 104 ", "Cl Blanco 1 ", "C 0001+557 ", "NGC 7826 ", "C \
0005+611 ", "[KPS2012] MWSC 0018 ", "Cl Berkeley 1 ", "C 0007+609 ", \
"Cl Berkeley 60 ", "C 0019+641 ", "[KPS2012] MWSC 0037 ", "C 0022+623 \
", "NGC 103 ", "Cl Berkeley 2 ", "NGC 110 ", "[KPS2012] MWSC 0046 ", \
"NGC 129 ", "C 0027+577 ", "NGC 133 ", "NGC 136 ", "C 0029+628 ", "Cl \
King 15 ", "NGC 146 ", "DSH J0033.1+6507 ", "NGC 189 ", "Cl Stock 24 \
", "Cl Dias 1 ", "NGC 225 ", "C 0040+598 ", "C 0040+639 ", "Cl \
Berkeley 4 ", "NGC 188 ", "Cl Berkeley 61 ", "C 0047+638 ", "C \
0048+579 ", "IC 1590 ", "Cl Berkeley 62 ", "Cl Czernik 3 ", "NGC 358 \
", "NGC 366 ", "NGC 381 ", "C 0109+620 ", "NAME HIP 5671 CLUSTER ", \
"NGC 433 ", "NGC 436 ", "NGC 457 ", "[KPS2012] MWSC 0120 ", "NGC 559 \
", "M 103 ", "Cl Czernik 4 ", "C 0132+610 ", "NGC 609 ", "NGC 637 ", \
"NGC 657 ", "NGC 654 ", "NGC 659 ", "C 0144+717 ", "NGC 663 ", "Cl \
Berkeley 5 ", "Cl Berkeley 6 ", "IC 166 ", "C 0149+568 ", "Cl \
Berkeley 7 ", "Cl Czernik 5 ", "NGC 752 ", "NGC 744 ", "NGC 743 ", \
"Cl Berkeley 8 ", "Cl Czernik 6 ", "Cl Czernik 7 ", "C 0200+642 ", \
"[KPS2012] MWSC 0165 ", "Riddle 4 ", "C 0211+590 ", "NGC 869 ", "C \
0215+580 ", "Cl Berkeley 63 "....} 



  LIST2 = `{{"Cl fug k9 ", {"C 2357+606", "OCl 277.0", "[KPR2004b] 1", 
   "Cl Berkeley 58", 
   "[KPS2012] MWSC 0001"}}, {"* gam Peg ", {"2MASS J00131414+1511008", "FK5 7", "PLX 33", 
      "* 88 Peg", "* gam Peg", "AAVSO 0008+14", "AG+14 11", "BD+14 14", 
      "CCDM J00132+1511A", "CSI+14 14 1", "CSV 100009", "GC 238", 
      "GCRV 105", "GEN# +1.00000886", "GSC 01179-01775", "HD 886", 
      "HGAM 5", "HIC 1067", "HIP 1067", "HR 39", "IDS 00080+1438 A", 
      "IRAS 00106+1454", "JP11 353", "N30 32", "NAME ALGENIB", "PHL 734", 
      "PMC 90-93 6", "PPM 116243", "ROT 69", "SAO 91781", "SKY# 390", 
      "SRS 30007", "TD1 78", "TYC 1179-1775-1", "UBV 128", "V* gam Peg", 
      "uvby98 100000886 V", "ALS 15090", "PLX 33.00", 
      "WDS J00132+1511A"}}, {"* zet Per ", {"PLX 856", "[GBA2007] 79", 
      "XEST 27-079", "* zet Per", "* 44 Per", "2E 902", "ADS 2843 A", 
      "AG+31 368", "BD+31 666", "CCDM J03541+3153A", "CSI+31 666 1", 
      "CSV 100357", "FK5 144", "GC 4688", "GCRV 2230", 
      "GEN# +1.00024398A", "GSC 02361-02820", "HD 24398", "HGAM 301", 
      "HIC 18246", "HIP 18246", "HR 1203", "IDS 03478+3135 A", 
      "IRC +30068", "JP11 828", "MCW 252", "N30 800", "NSV 1397", 
      "PMC 90-93 104", "PPM 68893", "ROT 574", "SAO 56799", "SKY# 5968", 
      "SRS 30144", "SV* ZI 260", "TD1 2623", "TYC 2361-2820-1", 
      "UBV 3778", "YPAC 14", "[C93] 149", "[KSP2003] G162.2896-16.6902", 
      "uvby98 100024398 A", "ALS 14763", "1RXS J035407.3+315255", 
      "2E 0350.9+3144", "[KSP2003] J035408.02+315300.2", 
      "2XMM J035407.8+315300", 
      "WDS J03541+3153A"}}, {"* eps Per ", {"PLX 869", "SBC9 2402", 
      "* 45 Per", "* eps Per", "AAVSO 0351+39", "ADS 2888 A", "AG+39 463",
       "ALS 7825", "BD+39 895", "CCDM J03579+4001A", "CEL 373", 
      "CSI+39 895 1", "CSV 100363", "EUVE J0357+40.0", "FK5 147", 
      "GC 4759", "GCRV 2266", "GEN# +1.00024760J", "GEN# +1.00024760A", 
      "GSC 02868-01623", "HD 24760", "HGAM 307", "HIC 18532", "HIP 18532",
       "HR 1220", "IDS 03511+3943 A", "JP11 837", "LS V +39 1", "MCW 257",
       "N30 815", "NSV 1423", "PMC 90-93 105", "PPM 46661", "ROT 3497", 
      "SAO 56840", "SKY# 6055", "SRS 30147", "SV* ZI 263", "TD1 2676", 
      "TYC 2868-1623-1", "UBV 3847", "V* eps Per", "uvby98 100024760 ABV",
       "WDS J03579+4001A", "IRAS 03544+3952", 
      "AKARI-IRC-V1 J0357512+400036", "PMSC 03511+3943A", 
      "PMSC 03511+3943Aabc"}}}

So in the LIST 1 is a set of strings names, LIST 2 is a set of sublists for certains string names, that could be the same OR NOT as in the LIST 1.

I need the following rationalite: If name 1 in the LIST 1, is the same as name "j" in the sublist of LIST 2, then take the element name "i" related to that sublist:

    (*   LIST 1 there is "Cl Berkeley 58 " , 
    and the its related "element" in the LIST 2 is:
     {"Cl fug k9  ", {"C 2357+606", "OCl 277.0", "[KPR2004b] 1", 
           "Cl Berkeley 58", 
           "[KPS2012] MWSC 0001"}} ,  
so take the element name "i" = "Cl fug k9  "*)

So I need to make a list of names that is composed by these "intersection names" , as:

 ListResulting={"Cl fug k9 ",............}

Summarizing, I am trying to filter the redundancies names in different catalogues,using the LIST1 as my reference catalogue and build a final one that includes all names with no redundancies. The sublists in LIST 2 are different names of the same object (name "i"). And I ll have to repeat these filtering for many list that has its own set of sublists, but always using the LIST 1 as the comparing list.

$\endgroup$
4
$\begingroup$

You can use Select:

With[{l1 = Map[StringTrim, LIST1, {-1}], l2 = Map[StringTrim, LIST2, {-1}]},
    Select[l2, Intersection[l1, #[[2]]] != {} &]]
(* {{"Cl fug k9", {"C 2357+606", "OCl 277.0", "[KPR2004b] 1", "Cl Berkeley 58", 
     "[KPS2012] MWSC 0001"}}} *)

Or Pick

With[{l1 = Map[StringTrim, LIST1, {-1}], l2 = Map[StringTrim, LIST2, {-1}]},
     Pick[l2, Intersection[l1, #] != {} & /@ l2[[All, 2]]]]
(* same output *)

If you want the first element of a sublist that satisfies the selection criterion:

With[{l1 = Map[StringTrim, LIST1, {-1}], l2 = Map[StringTrim, LIST2, {-1}]},
 Pick[l2[[All, 1]], Intersection[l1, #] != {} & /@ l2[[All, 2]]]]
(* {"Cl fug k9"} *)

More alternatives to specify the selector array in Pick:

With[{l1 = Map[StringTrim, LIST1, {-1}], l2 = Map[StringTrim, LIST2, {-1}]},
 Pick[l2[[All, 1]], (Or @@ StringMatchQ[#, Alternatives @@ l1]) & /@ l2[[All, 2]]]]
(*  {"Cl fug k9"} *)

With[{l1 = Map[StringTrim, LIST1, {-1}], l2 = Map[StringTrim, LIST2, {-1}]},
 Pick[l2[[All, 1]], (! FreeQ[#, Alternatives @@ l1]) & /@ l2[[All, 2]]]]
(*  {"Cl fug k9"} *)
$\endgroup$
  • $\begingroup$ Is fully operational, and I got what I need. Thanks kguler. $\endgroup$ – locometro Jan 11 '15 at 16:37
  • $\begingroup$ @locometro, my pleasure.. $\endgroup$ – kglr Jan 11 '15 at 16:40

Your Answer

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 or ask your own question.