fork download
  1. #arrの中から連続するnumの回数を配列にして返す
  2. def repeat_times(arr,num):
  3. res = []
  4. rp = 0
  5. for i in range(len(arr)):
  6. if arr[i] != num:
  7. if rp > 0:
  8. res.append(rp)
  9. rp = 0
  10. else:
  11. rp += 1
  12. if rp > 0:
  13. res.append(rp)
  14. return res
  15.  
  16. a = [0,1,1,1,0,0,0,0,1,1,0,1,1,1,1,1,0,1,0]
  17. ans = repeat_times(a,1)
  18. print(ans)
  19. ans = repeat_times(a,0)
  20. print(ans)
  21.  
Success #stdin #stdout 0.03s 9196KB
stdin
Standard input is empty
stdout
[3, 2, 5, 1]
[1, 4, 1, 1, 1]