5
$\begingroup$

Given two lists of string elements list1 and list2:

list1 = {"stel", "segw", "ma7", "str1"};
list2 = {
   "agr: Agriculture, hunting, forestry, fishing", 
   "min: Mining and quarrying", "ma1: Food, beverages, tobacco", 
   "ma2: Textiles, textile products", 
   "ma3: Leather, leather products, footwear", 
   "ma4: Wood & products of wood and cork", 
   "ma5: Pulp, paper, paper products, printing, publishing", 
   "ma6: Coke, refined petroleum, nuclear fuel", 
   "ma7: Chemicals & chemical products", 
   "ma8: Rubber-plastics products", 
   "ma9: Other non-metallic mineral products", 
   "ma10: Manufacture of basic metals", 
   "ma11: Machinery-equipment n.e.c", 
   "ma12: Electrical equipment", 
   "ma13: Transport equipment", 
   "ma14: Manufacturing, nec; recycling",
   "segw: Electricity, gas, water supply",
   "scst: Construction", 
   "ma15: Sale-maintenance; repair-install. of mach. equip.", 
   "swhl: Wholesale trade, except motor vehicles", 
   "srtl: Retail trade, except of motor vehicles", 
   "shot: Hotels & restaurants", "str1: Inland transport", 
   "str2: Water transport", 
   "str3: Air transport", 
   "str4: Other supporting-transport act. of travel agencies", 
   "stel: Post & telecommunications", 
   "sfin: Financial intermediation", 
   "sest: Real estate activities", 
   "sbus: Renting of M&Eq and other business activities", 
   "sadm: Public adm.-defence;compulsory social security", 
   "sedu: Education", 
   "shlt: Human health-social work", 
   "soth: Other community, social & personal services"
         };

I want to use list1 as the key to select the names after : in list2. The selection should be in the order of list1 as follows:

answer = {
  "Post & telecommunications",
  "Electricity, gas, water supply", 
  "Chemicals & chemical products",
  "Inland transport"
};
$\endgroup$

7 Answers 7

5
$\begingroup$

Use StringSplit to split values in list2, then use KeyTake to extract the indices you want.

Values@KeyTake[(Rule @@@ (StringSplit[#, ": "] & /@ list2)), list1]
$\endgroup$
5
$\begingroup$
(Association@(MapApply[Rule]@StringSplit[list2,":"]))/@list1

(* { Post & telecommunications, 
     Electricity, gas, water supply, 
     Chemicals & chemical products, 
     Inland transport}  *)
$\endgroup$
1
  • 2
    $\begingroup$ Seems the most intuitive for me. $\endgroup$ Sep 26 at 12:07
5
$\begingroup$
Lookup[list1] @ MapApply[Rule] @ StringSplit[list2, ":"]

{" Post & telecommunications", " Electricity, gas, water supply", "
Chemicals & chemical products", " Inland transport"}

$\endgroup$
1
  • $\begingroup$ very easy to understand (for me) and remember... $\endgroup$ Sep 26 at 12:52
4
$\begingroup$
StringCases[StringJoin[Riffle[list2, "\n"]], 
   RegularExpression[# <> ": (.+)"] -> "$1"] & /@ list1

{{"Post & telecommunications"}, {"Electricity, gas, water supply"},
{"Chemicals & chemical products"}, {"Inland transport"}}

$\endgroup$
2
$\begingroup$

A default clause is provided with the Lookup command, should you want to exploit it. Assuming:

list1 = {"stel", "segw", "ma7", "str1", "xyz"};
lut = Rule @@@ StringSplit[list2, ":"];
Lookup[lut, #, Framed@StringJoin[#, ":", "Not found"]] & /@ list1

enter image description here

$\endgroup$
2
$\begingroup$
(*Make a rule list*)
ruleList = Rule @@@ (StringSplit[#, ":"] & /@ list2);

(*Apply rules to list1*)
answer = list1 /. ruleList;

(*Remove leading and trailing white spaces*)
answer = StringTrim /@ answer;
$\endgroup$
2
$\begingroup$

My attempt using Select and StringStartsQ:

getDescriptions[code_List, descriptions_List] := 
Module[{order}, 
order = PositionIndex[code];
StringSplit[#, ": "][[2]] & /@ 
SortBy[Select[descriptions, StringStartsQ[#, Alternatives @@ code] &], 
order[First[StringSplit[#, ":"]]] &]]

Testing getDescriptions:

getDescriptions[list1, list2] === answer

(*True*)
$\endgroup$

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.

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