2
$\begingroup$

Im playing around with CountryData and I want to pass it 2 lists as argument, listCountries = {...} and data = {"GDP","ExternalDebt"}

If I do CountryData[#, "GDP"]& /@ listCountries it works well, but I want something like

CountryData[#1, #2]& /@ {listCountries,data}

I read something about MapThread but can make it.

$\endgroup$
1
  • $\begingroup$ Just did, it take the first 2 elements of the first list as input of #1 and #2 $\endgroup$ – Kafkarudo Jul 20 '14 at 21:55
3
$\begingroup$

Consider these two lists:

listCountries = {
   "Bangladesh",
   "India",
   "Monaco"
   };
data = {
   "GDP",
   "Population",
   "Area"
   };

There are two cases: you want the GDP, population and area of each country in the first list. The most elegant solution I can think of is Outer:

Outer[CountryData, listCountries, data]

enter image description here

The other is that you want the GDP of Bangladesh, the Population of India and the area of Monaco; this can be achieved by either

CountryData @@ # & /@ Thread@{listCountries, data}

enter image description here

or

MapThread[CountryData, {listCountries, data}]
$\endgroup$
2
  • $\begingroup$ Yes!, that would do. That was more than awesome, I love this forum and how you people gave answers by teaching.Thank you guys! $\endgroup$ – Kafkarudo Jul 20 '14 at 22:30
  • $\begingroup$ @Öskå hey, thanks! $\endgroup$ – acl Jul 20 '14 at 22:35
0
$\begingroup$

Would this work for you? Using Map twice feels like the most intuitive approach to me.

listCountries = {"United States", "Mexico", "Canada"};
data = {"GDP", "ExternalDebt"};
Function[{country}, CountryData[country, #] & /@ data] /@ listCountries

Mathematica graphics

If you want to display the country next to its data, just modify your Function to include that information.

listCountries = {"United States", "Mexico", "Canada"};
data = {"GDP", "ExternalDebt"};
Function[{country}, Prepend[CountryData[country, #] & /@ data, 
   country]] /@ listCountries

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ You can do Outer[{#1, CountryData[#1, #2]} &, listCountries, data] for the second bit. Outer is very flexible and well worth understanding. $\endgroup$ – acl Jul 21 '14 at 11:48
  • $\begingroup$ @acl Great tip! Thank you. $\endgroup$ – seismatica Jul 22 '14 at 3:56

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.