Rosetta Code/Rank languages by popularity: Difference between revisions

Content added Content deleted
(removed semicolon, ignore this. (This happens when you learn C++.))
(added enumerate + exaplanation)
Line 455: Line 455:


Fail-proof solution
Fail-proof solution

This solution is using "categoryinfo", this example will work with more than 500 categories (It does not read Special:Categories), and with more than 500 programming languages.


<lang python>import json
<lang python>import json
Line 463: Line 465:
langs = json.load(urllib.urlopen("http://www.rosettacode.org/w/api.php?\
langs = json.load(urllib.urlopen("http://www.rosettacode.org/w/api.php?\
action=query&list=categorymembers&cmtitle=Category:Programming_Languages\
action=query&list=categorymembers&cmtitle=Category:Programming_Languages\
&cmlimit=500&format=json"))
&cmlimit=500&format=json"));


titles = [i['title'] for i in langs['query']['categorymembers']]
titles = [i['title'] for i in langs['query']['categorymembers']]
Line 482: Line 484:
del titles[:50]
del titles[:50]


c = 0
for i in sorted(result,key=lambda x: x[1],reverse=True):
for n, i in enumerate(sorted(result,key=lambda x: x[1],reverse=True)):
print "%d. %s - %d" % (n+1, i[0].replace("Category:",''), i[1])</lang>
c += 1
print "%d. %s - %d" % (c, i[0].replace("Category:",''), i[1])</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==