I was writing Ruby recently, and I knew that it was Ruby.
test.rb
s = '123456789'
v = s.each_char.each_slice(3).map(&:join)
# ['123', '456', '789']
Cool ~
Is it like this in Python?
test.py
s = '123456789'
v = [s[i: i+3] for i in range(0, len(s), 3)]
# ['123', '456', '789']
When asked which one is better, I personally prefer Python, which can be combined with list comprehensions and general-purpose mechanisms, but Ruby is easier to understand. It's amazing that there is a method every time.
Recommended Posts