fork download
  1. def shape(lst):
  2. def ishape(lst):
  3. shapes = [ishape(x) if isinstance(x, list) else [] for x in lst]
  4. shape = shapes[0]
  5. if shapes.count(shape) != len(lst):
  6. raise ValueError('Ragged list')
  7. shape.append(len(lst))
  8. return shape
  9. return tuple(reversed(ishape(lst)))
  10.  
  11. print(shape([1, 2, 3, 4, 5, 6]))
  12. print(shape([[1, 2, 3], [4, 5, 6]]))
  13. print(shape([[1, 2], [3, 4], [5, 6]]))
  14. print(shape([[[1, 2, 3, 4, 5, 6]]]))
  15. print(shape([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]))
  16. print(shape([[1, 2, 3], [4, 5]]))
Runtime error #stdin #stdout #stderr 0.04s 9240KB
stdin
Standard input is empty
stdout
(6,)
(2, 3)
(3, 2)
(1, 1, 6)
(2, 2, 2)
stderr
Traceback (most recent call last):
  File "./prog.py", line 16, in <module>
  File "./prog.py", line 9, in shape
  File "./prog.py", line 6, in ishape
ValueError: Ragged list