官术网_书友最值得收藏!

  • Python Data Analysis
  • Ivan Idris
  • 127字
  • 2021-08-05 17:31:51

One-dimensional slicing and indexing

Slicing of one-dimensional NumPy arrays works just like the slicing of standard Python lists. Let's define an array containing the numbers 0, 1, 2, and so on up to and including 8. We can select a part of the array from indexes 3 to 7, which extracts the elements of the arrays 3 through 6 (have a look at slicing1d.py in this book's code bundle):

In: a = arange(9)
In: a[3:7]
Out: array([3, 4, 5, 6])

We can choose elements from indexes the 0 to 7 with an increment of 2:

In: a[:7:2]
Out: array([0, 2, 4, 6])

Just as in Python, we can use negative indices and reverse the array:

In: a[::-1]
Out: array([8, 7, 6, 5, 4, 3, 2, 1, 0])
主站蜘蛛池模板: 辛集市| 黄浦区| 湘乡市| 商南县| 夏津县| 民乐县| 翁牛特旗| 上杭县| 田东县| 镇远县| 沙田区| 双峰县| 南江县| 蓬安县| 莱芜市| 井冈山市| 绥棱县| 前郭尔| 凤山县| 张北县| 收藏| 榆林市| 仁寿县| 临海市| 博客| 贺州市| 台江县| 晋城| 大邑县| 宝清县| 江北区| 蚌埠市| 蒙山县| 崇文区| 长兴县| 高要市| 德江县| 凌海市| 余江县| 夏河县| 定西市|