I'm trying to find (a)which countries have dependencies (b)and a list of countries that consider themselves dependencies (c)and a list of countries that don't consider themselves dependencies.

Okay, so to find the countries that have dependencies (part a) I input

 CountryData["Countries", "Dependencies"]

But that output gives me {} throughout the list with the names of 10 or so countries, which I'm not sure are correct.

For part b, of finding the countries that consider themselves dependencies, I have:


for this part I'm not sure what else I am missing.

I got stuck at part b, so I think this part is similar to part c.


The dependencies refer to territories that are not official states of the governing countries, but are also not sovereign states in their own right. It's not clear what you intend to do with the mapping operation, but if I had to guess, you probably wanted to do something like this:

Map[{#, CountryData[#, "Dependencies"]} &, CountryData["Countries"]] /. 
    {_, {}} :> Sequence[]
(* {{"Australia", {"ChristmasIsland", "CocosKeelingIslands", "NorfolkIsland"}}, 
    {"China", {"HongKong", "Macau"}}, ... } *)

i.e., each country paired with its dependencies.

I prefer using rules for such purposes, so I would've probably written this as:

Thread[CountryData["Countries"] -> 
    CountryData["Countries", "Dependencies"]] ~DeleteCases~ Rule[_, {}]
(* {"Australia" -> {"ChristmasIsland", "CocosKeelingIslands", "NorfolkIsland"}, 
    "China" -> {"HongKong", "Macau"}, ... } *)

You can also do this the other way round – a rule mapping each dependency to its governing state:

Thread /@ Thread[CountryData["Countries", "Dependencies"] -> 
     CountryData["Countries"]] ~DeleteCases~ Rule[{}, _] // Flatten
(* {"ChristmasIsland" -> "Australia", "CocosKeelingIslands" -> "Australia", 
    "NorfolkIsland" -> "Australia", "HongKong" -> "China", "Macau" -> "China", ... *)
  • $\begingroup$ I haven't gotten to the point of using the command thread yet, but I'll look out for it. as for your Map function, what does this mean: /. {_, {}} :> Sequence[] $\endgroup$ – asik Oct 23 '13 at 23:53
  • $\begingroup$ @asik Lookup ReplaceAll. In short, it removes sublists of length 2 that have a {} as the second element. $\endgroup$ – rm -rf Oct 24 '13 at 0:08
  • $\begingroup$ Okay, thanks. I think it was the Sequence part that threw me off originally. $\endgroup$ – asik Oct 24 '13 at 0:11
  • $\begingroup$ Also, is there a way to check out which countries aren't dependencies? I tried using the != notion since I couldn't think of something that would counter the input for part b. $\endgroup$ – asik Oct 24 '13 at 0:12
  • $\begingroup$ @asik Countries aren't dependencies by definition... did you mean to find which countries don't have dependencies? $\endgroup$ – rm -rf Oct 24 '13 at 0:17

here's a possible way:

With[{cd = CountryData[]}, 
  {#[[All, 1]], Flatten@#[[All, 2]], Complement[cd, Flatten@#[[All, 2]]]} &
  @(Select[#, #[[2]] != {} &] &@({#, CountryData[#, "Dependencies"]} & /@ cd))]

Note that it is essentially based on

(Select[#, #[[2]] != {} &] &@({#, CountryData[#, "Dependencies"]} & /@ CountryData[]))

Which gives a nested list of countries and its dependencies.


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.