I usually write Ruby, but recently I started to touch Python as a hobby. I found it useful to know a slice of a Python list. By the way, I was wondering how to write ʻarray [:: 3]` in Python in Ruby, so I compared them.
Python
n = 3
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array[::n] #=> [0, 3, 6, 9]
Ruby
n = 3
array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
array.select.with_index { |_, i| i % n == 0 } #=> [0, 3, 6, 9]
Hmmm, is there a better way to write it ...: thinking:
Recommended Posts