Advertisement
campos20

Avg name count per country

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