python里的i_Python 中[::] 与 [:,:,i] 总结

最近在学 吴恩达的 Deep Learning 中的第五门课 Sequence Model, 第一个lab是用 Numpy 搭建 RNN,在搭建RNN的时候用到了 Numpy的 Slicing ([:,:,i]) , 在这里想总结下[:,:,i] 与 [::i]的用法,有写的不对的地方请随时指教。

总的来说,[::i] 是Python中的基础索引,而[:,:,i]是Numpy 中对于多维度 Array的提取,在Stack Overflow 中关于Slice的一个回答说到:Extended slicing (with commas and ellipses) are mostly used only by special data structures (like NumPy); the basic sequences don't support them.

Slice基本用法:

>>> seq[:] # [seq[0], seq[1], ..., seq[-1] ]

>>> seq[low:] # [seq[low], seq[low+1], ..., seq[-1] ]

>>> seq[:high] # [seq[0], seq[1], ..., seq[high-1]]

>>> seq[low:high] # [seq[low], seq[low+1], ..., seq[high-1]]

>>> seq[::stride] # [seq[0], seq[stride], ..., seq[-1] ]

>>> seq[low::stride] # [seq[low], seq[low+stride], ..., seq[-1] ]

>>> seq[:high:stride] # [s

你可能感兴趣的:(python里的i)