fork download
  1. # -*- coding: utf8 -*-
  2. import csv, itertools, json
  3.  
  4. def cluster(rows):
  5. result = []
  6. data = sorted(rows, key=lambda r: r[1])
  7. for k, g in itertools.groupby(rows, lambda r: r[0]):
  8. group_rows = [row[1:] for row in g]
  9.  
  10. if len(row[1:]) == 1:
  11. result.append({"round": row[0],"winner": int(row[1]),"points": int(row[2]),"year": int(row[3])})
  12.  
  13. else:
  14. result.append({"round": k,"children":cluster(group_rows)})
  15.  
  16. return result
  17.  
  18. if __name__ == '__main__':
  19. s = '''\
  20. Final Rafael Nadal 7-5 3-6 7-6(3) 3-6 6-2 2009
  21. semi Rafael Nadal 6-7(4) 6-4 7-6(2) 6-7(1) 6-4 2009
  22. semi Roger Federer 6-2 7-5 7-5 2009
  23. quarter Rafael Nadal 6-2 7-5 7-5 2009
  24. quarter Fernando Verdasco 7-6(2) 3-6 6-3 6-2 2009
  25. quarter Roger Federer 6-3 6-0 6-0 2009
  26. quarter Andy Roddick 6-7(3) 6-4 6-2 2-1 ret. 2009
  27. Fourth Rafael Nadal 6-3 6-2 6-4 2009
  28. Fourth Jo-Wilfried Tsonga 6-4 6-4 7-6(3) 2009
  29. Fourth Gilles Simon 6-4 2-6 6-1 ret. 2009
  30. Fourth Fernando Verdasco 2-6 6-1 1-6 6-3 6-4 2009
  31. Fourth Juan Martin Del Potro 5-7 6-4 6-4 6-2 2009
  32. Fourth Andy Roddick 7-5 6-1 6-3 2009
  33. Fourth Roger Federer 4-6 6-7(4) 6-4 6-4 6-2 2009
  34. Fourth Novak Djokovic 6-1 7-6(1) 6-7(5) 6-2 2009
  35. Third James Blake 6-3 6-2 3-6 6-1 2009
  36. Third Fernando Gonzalez 3-6 3-6 7-6(10) 6-2 12-10 2009
  37. Third Gael Monfils 6-4 6-3 7-5 2009
  38. Third Fernando Verdasco 6-4 6-0 6-0 2009
  39. Third Jo-Wilfried Tsonga 6-4 6-2 1-6 6-1 2009
  40. Third Andy Murray 7-5 6-0 6-3 2009
  41. Third Rafael Nadal 6-4 6-2 6-2 2009
  42. Third Gilles Simon 7-6(2) 6-4 6-2 2009
  43. Third Tommy Robredo 6-1 6-3 6-2 2009
  44. Third Andy Roddick 6-3 6-4 6-2 2009
  45. Third Marin Cilic 7-6(5) 6-3 6-4 2009
  46. Third Marcos Baghdatis 6-2 6-4 6-4 2009
  47. Third Tomas Berdych 4-6 6-1 6-3 6-4 2009
  48. Third Novak Djokovic 6-2 4-6 6-3 7-6(4) 2009
  49. Third Roger Federer 6-3 6-2 7-6(5) 2009
  50. Third Juan Martin Del Potro 6-7(5) 7-5 6-3 7-5 2009
  51. Second Radek Stepanek 6-3 6-2 6-7(3) 7-5 2009
  52. Second Richard Gasquet 6-3 6-4 6-4 2009
  53. Second Nicolas Almagro 6-2 7-5 6-0 2009
  54. Second Andy Murray 6-4 6-2 6-2 2009
  55. Second James Blake 6-3 6-2 6-3 2009
  56. Second Jurgen Melzer 5-7 7-6(7) 6-4 6-3 2009
  57. Second Mario Ancic 5-7 7-5 4-6 6-4 6-3 2009
  58. Second Igor Andreev 6-4 6-4 5-7 3-6 6-4 2009
  59. Second Fernando Verdasco 6-1 6-1 6-2 2009
  60. Second Dudi Sela 6-3 6-3 6-2 2009
  61. Second Tommy Haas 6-1 6-2 6-1 2009
  62. Second Gilles Simon 6-7(5) 6-4 6-1 6-2 2009
  63. Second Jo-Wilfried Tsonga 6-7(4) 7-6(8) 7-6(7) 6-2 2009
  64. Second Fernando Gonzalez 7-5 6-3 6-4 2009
  65. Second Rafael Nadal 6-2 6-3 6-2 2009
  66. Second Gael Monfils 6-4 6-4 3-6 6-2 2009
  67. Second Marat Safin 7-5 6-2 6-2 2009
  68. Second Mardy Fish 6-4 6-1 7-5 2009
  69. Second Tomas Berdych 6-1 6-1 6-3 2009
  70. Second Stanislas Wawrinka 6-3 6-4 6-4 2009
  71. Second Marcos Baghdatis 3-6 7-5 6-3 6-3 2009
  72. Second David Ferrer 6-2 6-2 6-1 2009
  73. Second Novak Djokovic 7-5 6-1 6-3 2009
  74. Second Tommy Robredo 6-1 6-3 6-0 2009
  75. Second Andy Roddick 4-6 6-2 7-6(1) 6-2 2009
  76. Second Fabrice Santoro 5-7 7-5 3-6 7-5 6-3 2009
  77. Second Roger Federer 6-2 6-3 6-1 2009
  78. Second Amer Delic 1-6 3-6 6-3 7-6(3) 9-7 2009
  79. Second Yen-Hsun Lu 6-4 5-7 4-6 6-4 6-2 2009
  80. Second Gilles Muller 3-6 6-1 6-4 6-2 2009
  81. Second Marin Cilic 6-2 6-3 4-6 6-3 2009
  82. Second Juan Martin Del Potro 6-1 7-5 6-2 2009
  83. First James Blake 6-4 6-3 7-5 2009
  84. First Roko Karanusic 6-3 1-6 6-3 3-6 6-3 2009
  85. First Fernando Gonzalez 5-7 6-2 6-2 3-6 6-3 2009
  86. First Andy Murray 6-2 3-1 ret. 2009
  87. First Radek Stepanek 3-6 6-3 6-4 6-4 2009
  88. First Victor Hanescu 3-6 3-6 7-6(7) 7-6(4) 8-6 2009
  89. First Stefan Koubek 6-3 6-2 6-2 2009
  90. First Ernests Gulbis 6-3 6-2 6-3 2009
  91. First Mario Ancic 7-5 6-4 4-6 6-2 2009
  92. First Fernando Verdasco 6-0 6-2 6-2 2009
  93. First Arnaud Clement 6-3 2-6 4-6 6-2 6-1 2009
  94. First Michael Berrer 6-2 6-4 6-3 2009
  95. First Andreas Beck 7-5 6-1 6-0 2009
  96. First Tommy Haas 6-3 6-3 6-4 2009
  97. First Denis Istomin 6-2 7-5 6-4 2009
  98. First Gael Monfils 6-1 6-3 7-5 2009
  99. First Igor Andreev 5-7 3-6 6-4 6-3 6-4 2009
  100. First Sebastien de Chaunac 2-6 6-3 0-6 6-2 6-2 2009
  101. First Ivan Ljubicic 4-6 7-6(3) 7-6(7) 5-7 6-3 2009
  102. First Fabio Fognini 3-6 7-6(7) 6-4 6-2 2009
  103. '''
  104. rows = list(csv.reader(s.splitlines()))
  105. print json.dumps(cluster(rows),indent=2)
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
2
5
7 7 2 11 7
10
9 1 7 6 9 9 8 7 6 7
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.4/py_compile.py", line 124, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap>", line 1532, in source_to_code
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "./prog.py", line 105
    print json.dumps(cluster(rows),indent=2)
             ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.4/py_compile.py", line 128, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 105
    print json.dumps(cluster(rows),indent=2)
             ^
SyntaxError: invalid syntax

stdout
Standard output is empty