Advertisement
campos20

avg number of comps per country since 2010 in WCA

Jun 20th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. # this program and the extract of the wca export must be in the same folder
  2. # to run, just type "python program.py"
  3.  
  4. # avg number of comps per country since 2010
  5.  
  6. # sum the number of competitions from 2010 and 2018 and divide by 9 seems booring,
  7. # so we are taking the average of the number of comps/year since 2010.
  8.  
  9. import csv
  10.  
  11. country_list = []
  12. comp_year = []
  13. avg_list = []
  14.  
  15. base_year = 2010
  16. count = 0
  17. discard = 1 # ignore countries with 1 competition
  18.  
  19. def avg(l):
  20.     out = 0.0
  21.     if len(l)==0: return 0.0
  22.     for x in l:
  23.         out += x
  24.     return 1.0*out/len(l)
  25.  
  26. with open('WCA_export_Competitions.tsv','rb') as tsvin:
  27.     tsvin = csv.reader(tsvin, delimiter='\t')
  28.            
  29.     for line in tsvin:
  30.  
  31.         count += 1
  32.         if count == 1: continue # discard the header. I'm too lazy to search for a better way to do this.
  33.    
  34.         country = line[3]
  35.        
  36.         year = int(line[5])
  37.        
  38.         if year < base_year: continue
  39.        
  40.         if country not in country_list:
  41.             country_list.append(country)
  42.             comp_year.append([])
  43.        
  44.         i = country_list.index(country)
  45.        
  46.         while len(comp_year[i]) < year-base_year+1:
  47.             comp_year[i].append(0)
  48.        
  49.         comp_year[i][year-base_year] += 1 # 2010 is 0, 2011 is 1, etc. we add 1 for each comp
  50.        
  51.     for x in comp_year:
  52.         avg_list.append(avg(x))
  53.    
  54.     prev = 0 # tied stats
  55.     count = 1
  56.     for (x, y, z) in sorted(zip(avg_list, country_list, comp_year))[::-1]:
  57.         if sum(z) <= discard: continue
  58.         print "%3s"%(str(count).zfill(2)+")" if x!=prev else ""), ("%.2f"%x).zfill(5), y, z
  59.         count += 1
  60.         prev = x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement