Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
Just did, it take the first 2 elements of the first list as input of #1 and #2 –  Kafkarudo Jul 20 at 21:55

2 Answers 2

up vote 3 down vote accepted

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}]
share|improve this answer
    
Yes!, that would do. That was more than awesome, I love this forum and how you people gave answers by teaching.Thank you guys! –  Kafkarudo Jul 20 at 22:30
    
@Öskå hey, thanks! –  acl Jul 20 at 22:35

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

share|improve this answer
    
You can do Outer[{#1, CountryData[#1, #2]} &, listCountries, data] for the second bit. Outer is very flexible and well worth understanding. –  acl Jul 21 at 11:48
    
@acl Great tip! Thank you. –  seismatica Jul 22 at 3:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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