campos20

Avg name count per country

Feb 3rd, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. # Alexandre Campos, 02/03/2019
  2. # program made for the WCA Statistics
  3. # place this program alongside with the tsv (unziped) export from
  4. # https://www.worldcubeassociation.org/results/misc/WCA_export.tsv.zip
  5. # and run it with
  6. # python program.py
  7.  
  8. import csv
  9.  
  10. def show_results(labels, results_list, list_of_lists, limit = float("inf"), direction = "asc", string_format = "%s"):
  11.     """'Labels is' intuitive. Results_list is the main order list and list_of_lists are other lists that are gonna be ordered as well (eg.: country, name, etc). You can set limit if you wish. Direction = 'asc' if you wanna the lower results first; direction = 'des' if you wanna bigger results first."""
  12.  
  13.     pos = 1
  14.  
  15.     prev = None
  16.    
  17.     out = "Pos"
  18.     for x in labels:
  19.         out += "\t"+x
  20.     print out
  21.    
  22.     temp = []
  23.     if direction == "asc":
  24.         temp = sorted(zip(results_list, range(len(results_list))))
  25.     elif direction == "des":
  26.         temp = sorted(zip(results_list, range(len(results_list))))[::-1]
  27.     else:
  28.         raise ValueError, "Invalid direction: %s"%direction
  29.  
  30.     for a, b in temp:
  31.         out = ""
  32.         if a != prev:
  33.             if pos > limit:
  34.                 break
  35.             out+="%s)\t"%pos
  36.         else:
  37.             out += "---\t"
  38.         out += format(a, string_format)+"\t"
  39.         for x in list_of_lists:
  40.             out += str(x[b])+"\t"
  41.         print out.strip()
  42.         prev = a
  43.         pos += 1
  44.  
  45. def avg(l):
  46.     if len(l) == 0: return 0.
  47.     return 1.0*sum(l)/len(l)
  48.  
  49. def main():
  50.     country_list = []
  51.     count = []
  52.    
  53.     header = True
  54.  
  55.     with open("WCA_export_Persons.tsv") as tsvfile:
  56.  
  57.         tsvreader = csv.reader(tsvfile, delimiter="\t")
  58.         for line in tsvreader:
  59.        
  60.             if header:
  61.                 header = False
  62.                 continue
  63.        
  64.             name = line[2]
  65.             if "(" in name:
  66.                 name = name[:name.index("(")]
  67.            
  68.             name_count = len(name.split())
  69.             country = line[3]
  70.            
  71.             i = -1
  72.             try:
  73.                 i = country_list.index(country)
  74.             except:
  75.                 country_list.append(country)
  76.                 count.append([])
  77.             count[i].append(name_count)
  78.        
  79.     avg_list = map(avg, count)
  80.     no_of_competitors = map(len, count)
  81.     labels = ["Avg", "Country", "# of competitors"]
  82.     list_of_lists = [country_list, no_of_competitors]
  83.    
  84.     assert len(labels) == len(list_of_lists)+1
  85.    
  86.     limit = 1000
  87.     show_results(labels, avg_list, list_of_lists, limit, "up", ".2f")
  88.            
  89. main()
Advertisement
Add Comment
Please, Sign In to add comment