Recommended syntax for slicing arrays #2186
-
I use Hy for data science. This requires a lot of cutting/slicing. In particular, I frequently have to cut a list or an array from an index until the end. In Python, the way to slice an array from an index to the end is to leave the second index argument of a slice empty as in This no longer works in Hy's alpha. According to the Hy alpha's changelog, " What is the recommended method for getting everything after the first |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
(setv x [1 2 3 4 5 6 7])
(get x (slice 5 None))
;; => [6 7]
(cut x 5 None)
;; => [6 7] x[5:]
# => [6, 7]
x[slice(5, None)]
# => [6, 7] If you need more complex slicing mechanics (or more "Pythonic"), and you're willing to use the master branch Hy version, you can check out Hyrule's (get x #: 5:)
;; => [6 7]
(ncut x 5:)
;; => [6 7] |
Beta Was this translation helpful? Give feedback.
-
Thank you so much. I didn't realize I could add |
Beta Was this translation helpful? Give feedback.
cut
was changed to have the same syntax asslice
which is what python's:
sugar compiles down to. Soarr[5:]
actually compiles down toarr[slice(5, None)]
. Now that cut is equivalent all the following are identical.If you need more complex slicing mechanics (or more "Pythonic"), and you're willing to use the master branch Hy version, you can check out Hyrule's
#:
andncut
macros. Specificallyncut
for you since it allows the sugared n-dimensional slicing thatnumpy
andpandas
use frequently