Skip to content

Instantly share code, notes, and snippets.

@domdip
Created January 27, 2013 06:11
Show Gist options
  • Save domdip/4646822 to your computer and use it in GitHub Desktop.
Save domdip/4646822 to your computer and use it in GitHub Desktop.
Of countries that contain megacities (urbanized areas >= 10M), what percentage of their population lives in such areas? Uses 2012 estimates from Demographia World Urban Areas and Wikipedia. Output in comments toward the bottom.
#!/usr/bin/python
from collections import defaultdict
def main():
#From http://www.newgeography.com/content/002808-world-urban-areas-population-and-density-a-2012-update
citydata = """
1 Japan Tokyo-Yokohama 37,126,000 3,300 11,300 8,547 4,300
2 Indonesia Jakarta 26,063,000 1,075 24,200 2,784 9,400
3 South Korea Seoul-Incheon 22,547,000 835 27,000 2,163 10,400
4 India Delhi, DL-HR-UP 22,242,000 750 29,700 1,943 11,500
5 Philippines Manila 21,951,000 550 39,900 1,425 15,400
6 China Shanghai, SHG 20,860,000 1,350 15,500 3,497 6,000
7 United States New York, NY-NJ-CT 20,464,000 4,495 4,600 11,642 1,800
8 Brazil Sao Paulo 20,186,000 1,225 16,500 3,173 6,400
9 Mexico Mexico City 19,463,000 790 24,600 2,046 9,500
10 Egypt Cairo 17,816,000 660 27,000 1,709 10,400
11 China Beijing, BJ 17,311,000 1,350 12,800 3,497 5,000
12 Japan Osaka-Kobe-Kyoto 17,011,000 1,240 13,700 3,212 5,300
13 India Mumbai, MAH 16,910,000 211 80,100 546 30,900
14 China Guangzhou-Foshan, GD 16,827,000 1,225 13,700 3,173 5,300
15 Russia Moscow 15,512,000 1,700 9,100 4,403 3,500
16 Bangladesh Dhaka 15,414,000 134 115,000 347 44,400
17 United States Los Angeles, CA 14,900,000 2,432 6,100 6,299 2,400
18 India Kolkota, WB 14,374,000 465 30,900 1,204 11,900
19 Pakistan Karachi 14,198,000 300 47,300 777 18,300
20 Argentina Buenos Aires 13,639,000 1,020 13,400 2,642 5,200
21 Turkey Istanbul 13,576,000 540 25,100 1,399 9,700
22 Brazil Rio de Janeiro 12,043,000 780 15,400 2,020 6,000
23 China Shenzhen, GD 11,885,000 675 17,600 1,748 6,800
24 Nigeria Lagos 11,547,000 350 33,000 907 12,700
25 France Paris 10,755,000 1,098 9,800 2,844 3,800
26 Japan Nagoya 10,027,000 1,475 6,800 3,820 2,600
"""
#From http://en.wikipedia.org/wiki/List_of_countries_by_population
countrydata = """
1 China[7] 1,354,040,000 December 31, 2012 19.17% Official estimate
2 India 1,210,193,422 March 1, 2011 17.14% 2011 census
3 United States 315,224,000 January 27, 2013 4.46% Official population clock
4 Indonesia 237,641,326 May 1, 2010 3.36% 2010 census
5 Brazil 193,946,886 July 1, 2012 2.75% Official estimate
6 Pakistan 182,063,000 January 27, 2013 2.58% Official population clock
7 Nigeria 166,629,000 July 1, 2012 2.36% UN estimate
8 Bangladesh 152,518,015 July 16, 2012 2.16% Official estimate
9 Russia 143,369,806 January 1, 2013 2.03% Official estimate
10 Japan 127,520,000 December 1, 2012 1.81% Monthly official estimate
11 Mexico 112,336,538 June 12, 2010 1.59% 2010 census
12 Philippines 92,337,852 May 1, 2010 1.31% 2010 census
13 Vietnam 87,840,000 July 1, 2011 1.24% Official estimate
14 Ethiopia 84,320,987 July 1, 2012 1.19% Official estimate
15 Egypt 83,819,000 January 27, 2013 1.19% Official population clock
16 Germany 81,923,000 July 31, 2012 1.16% Monthly official estimate
17 Iran 77,105,000 January 27, 2013 1.09% Official population clock
18 Turkey 74,724,269 December 31, 2011 1.06% Official estimate
19 Democratic Republic of the Congo 69,575,000 July 1, 2012 0.99% UN estimate
20 Thailand 65,926,261 September 1, 2010 0.93% 2010 census
21 France[8] 65,635,000 November 1, 2012 0.93% Monthly official estimate
22 United Kingdom 63,181,775 March 27, 2011 0.89% 2011 census
23 Italy 60,870,745 June 30, 2012 0.86% Monthly official estimate
24 South Africa 51,770,560 October 9, 2011 0.73% 2011 census
25 South Korea 50,004,441 July 1, 2012 0.71% Official estimate
26 Myanmar 48,724,000 July 1, 2012 0.69% UN estimate
27 Colombia 46,894,000 January 27, 2013 0.66% Official population clock
28 Spain 46,815,916 November 1, 2011 0.66% 2011 census
29 Ukraine 45,560,255 November 1, 2012 0.65% Monthly official estimate
30 Tanzania 44,929,002 August 26, 2012 0.64% 2012 census
31 Argentina 40,117,096 October 27, 2010 0.57% 2010 census
32 Kenya 38,610,097 August 24, 2009 0.55% 2009 census
33 Poland 38,317,000 July 1, 2012 0.54% Official estimate
34 Algeria 37,800,000 January 1, 2013 0.54% Official estimate
35 Canada 35,002,447 October 1, 2012 0.5% Official estimate
36 Uganda 34,131,400 July 1, 2012 0.48% Official estimate
37 Iraq 33,330,000 July 1, 2011 0.47% Official estimate
38 Morocco 32,800,100 January 27, 2013 0.46% Official population clock
39 Sudan 30,894,000 April 22, 2008 0.44% 2008 census
40 Peru 30,135,875 June 30, 2012 0.43% Official estimate
41 Malaysia 29,584,000 January 27, 2013 0.42% Official population clock
42 Uzbekistan 29,559,100 January 1, 2012 0.42% Official estimate
43 Venezuela 28,946,101 October 30, 2011 0.41% 2011 census
44 Saudi Arabia 28,376,355 July 1, 2011 0.4% Official estimate
45 Nepal 26,494,504 June 22, 2011 0.38% 2011 census
46 Afghanistan 25,500,100 January 1, 2013 0.36% Official estimate
47 Ghana 24,658,823 September 26, 2010 0.35% 2010 census
48 North Korea 24,554,000 July 1, 2012 0.35% Official estimate
49 Yemen 24,527,000 July 1, 2012 0.35% Official estimate
50 Mozambique 23,700,715 July 1, 2012 0.34% Official estimate
51 Taiwan[9] 23,305,021 November 30, 2012 0.33% Monthly official estimate
52 Australia 22,885,049 January 27, 2013 0.32% Official population clock
53 Syria 21,941,000 January 27, 2013 0.31% Official population clock
54 Ivory Coast 21,395,000 July 1, 2009 0.3% Official estimate
55 Madagascar 20,696,070 July 1, 2011 0.29% Official estimate
56 Angola 20,609,300 July 1, 2012 0.29% Official estimate
57 Sri Lanka 20,277,597 March 20, 2012 0.29% 2012 census
58 Cameroon 19,406,100 January 1, 2010 0.27% Official estimate
59 Romania 19,043,767 October 20, 2011 0.27% 2011 census
60 Kazakhstan 16,897,000 December 1, 2012 0.24% Monthly official estimate
61 Netherlands 16,775,273 November 30, 2012 0.24% Monthly official estimate
62 Chile 16,572,475 April 9, 2012 0.23% 2012 census
63 Niger 16,274,738 July 1, 2012 0.23% Official estimate
64 Malawi 15,883,000 July 1, 2012 0.22% UN estimate
65 Burkina Faso 15,730,977 July 1, 2010 0.22% Official estimate
66 Ecuador 15,410,700 January 27, 2013 0.22% Official population clock
67 Guatemala 14,713,763 July 1, 2011 0.21% Official estimate
68 Mali 14,528,662 April 1, 2009 0.21% 2009 census
69 Cambodia 14,478,000 July 1, 2012 0.21% UN estimate
70 Zambia 13,092,666 October 16, 2010 0.19% 2010 census
71 Zimbabwe 12,973,808 August 17, 2012 0.18% 2012 census
72 Senegal 12,855,153 July 1, 2011 0.18% Official estimate
73 Chad 11,274,106 May 20, 2009 0.16% 2009 census
74 Cuba 11,163,934 September 15, 2012 0.16% 2012 census
75 Belgium 11,139,292 November 1, 2012 0.16% Official estimate
76 Guinea 10,824,200 July 1, 2010 0.15% Official estimate
77 Greece 10,815,197 May 24, 2011 0.15% 2011 census
78 Tunisia 10,777,500 July 1, 2012 0.15% Official estimate
79 Portugal 10,562,178 March 21, 2011 0.15% 2011 census
80 Rwanda 10,537,222 August 15, 2012 0.15% 2012 census
81 Czech Republic 10,513,209 September 30, 2012 0.15% Official estimate
82 Bolivia 10,426,155 July 1, 2010 0.15% Official estimate
83 Haiti 10,085,214 July 1, 2010 0.14% Official estimate
"""
#Work with the megacity pop data
parsedrows = [[item.strip() for item in row.split('\t')] for row in citydata.splitlines()[1:]]
urbanpoppair = [(row[1], float(row[3].replace(',',''))) for row in parsedrows]
country, urbanpop = zip(*urbanpoppair)
#Get unique countries under consideration
countryset = set(country)
#Add together megacities for the same country
totalurbanpop = defaultdict(float)
for c,u in urbanpoppair:
totalurbanpop[c] += u
totalurbanpop = dict(totalurbanpop)
#Work with the total country population data
parsedrows = [[item.strip() for item in row.split('\t')] for row in countrydata.splitlines()[1:]]
countrypoppair = [(row[1].partition('[')[0], float(row[2].replace(',',''))) for row in parsedrows]
countrypopdict = dict(countrypoppair)
percurban = [(totalurbanpop[country] / countrypopdict[country],country) for country in countryset if country in countrypopdict]
percurban = sorted(percurban)
percurban.reverse()
for perc, city in percurban:
print "{:.2%} of the population of {} is in a megacity.".format(perc,city)
#Output:
# 50.32% of the population of Japan is in a megacity.
# 45.09% of the population of South Korea is in a megacity.
# 34.00% of the population of Argentina is in a megacity.
# 23.77% of the population of Philippines is in a megacity.
# 21.26% of the population of Egypt is in a megacity.
# 18.17% of the population of Turkey is in a megacity.
# 17.33% of the population of Mexico is in a megacity.
# 16.62% of the population of Brazil is in a megacity.
# 16.39% of the population of France is in a megacity.
# 11.22% of the population of United States is in a megacity.
# 10.97% of the population of Indonesia is in a megacity.
# 10.82% of the population of Russia is in a megacity.
# 10.11% of the population of Bangladesh is in a megacity.
# 7.80% of the population of Pakistan is in a megacity.
# 6.93% of the population of Nigeria is in a megacity.
# 4.94% of the population of China is in a megacity.
# 4.42% of the population of India is in a megacity.
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment