1-D Array

Indexing

Use bracket notation [ ] to get the value at a specific index. Remember that indexing starts at 0.

 import numpy as np
a=np.arange(12)
a
# start from index 0
a[0]
# the last element
a[-1]

Output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

0

11

Slicing

Use : to indicate a range.

array[start:stop] 

A second : can be used to indicate step-size.

array[start:stop:stepsize]

Leaving start or stop empty will default to the beginning/end of the array.

 a[1:4]
a[-4:]
a[-5::-2] #starting 5th element from the end, and counting backwards by 2 until the beginning of the array is reached

Output:

array([1, 2, 3, 4])

array([ 8,  9, 10, 11])

array([7, 5, 3, 1])

Multidimensional Array

 r = np.arange(36)
r.resize((6, 6))
r

Output:

array([[ 0,  1,  2,  3,  4,  5],

[ 6,  7,  8,  9, 10, 11],

[12, 13, 14, 15, 16, 17],

[18, 19, 20, 21, 22, 23],

[24, 25, 26, 27, 28, 29],

[30, 31, 32, 33, 34, 35]])

Use bracket notation to index:

array[row, column] 

and use : to select a range of rows or columns

 r[2, 2]
r[3, 3:6]
r[:2, :-1]#selecting all the rows up to (and not including) row 2, and all the columns up to (and not including) the last column
r[-1, ::2]#selecting the last row, and only every other element

Output:

14

array([21, 22, 23])

array([[ 0,  1,  2,  3,  4],

[ 6,  7,  8,  9, 10]])

array([30, 32, 34])

We can also select nonadjacent elements by

r[[2,3],[4,5]] 

Output:

array([16, 23])

Conditional Indexing

r[r > 30]

Output:

array([31, 32, 33, 34, 35])

Note that if you change some elements in the slice of an array, the original array will also be change. You can see the following example:

 r2 = r[:3,:3]
print(r2)
print(r)
r2[:] = 0
print(r2)
print(r)

Output:

[[ 0  1  2]

[ 6  7  8]

[12 13 14]]

[[ 0  1  2  3  4  5]

[ 6  7  8  9 10 11]

[12 13 14 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

[[0 0 0]

[0 0 0]

[0 0 0]]

[[ 0  0  0  3  4  5]

[ 0  0  0  9 10 11]

[ 0  0  0 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

To avoid this, use r.copy to create a copy that will not affect the original array.

 r_copy = r.copy()
print(r_copy, '\n')
r_copy[:] = 10
print(r_copy, '\n')
print(r)

Output:

[[ 0  0  0  3  4  5]

[ 0  0  0  9 10 11]

[ 0  0  0 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

[[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]]

[[ 0  0  0  3  4  5]

[ 0  0  0  9 10 11]

[ 0  0  0 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

最新文章

  1. 第20讲 HOOK和数据库编程
  2. jquery中bind()绑定多个事件
  3. 基于 AVPlayer 自定义播放器
  4. 【leetcode】Climbing Stairs
  5. 用友android
  6. Hibernate之Query接口的uniqueResult()方法
  7. Java Hour 38 Weather ( 11 ) – fastjson
  8. Android 利用ListView制作带竖线的多彩表格
  9. 【测试】使用hr用户下的employees表写一条SQL语句,执行计划走索引全扫描
  10. HDU3507 Print Article(斜率优化dp)
  11. Cllimbing Stairs [LeetCode 70]
  12. dorado7 重装了tomcat后配置路径
  13. 在网页上看到想要的颜色,如何知道这种颜色的颜色代码和 RGB 颜色值?
  14. #define is unsafe
  15. IT服务(运维)管理实施的几个要点--序言
  16. 原生 js 实现点击按钮复制文本
  17. Qt 地址薄 (二) 添加地址
  18. oracle中tables和views的区别
  19. 被fancybox坑的心路历程
  20. 【转】C# string数组转int数组

热门文章

  1. poj 3292 H-素数问题 扩展艾氏筛选法
  2. HOJ 13845 Atomic Computer有向无环图的动态规划
  3. linux系统下单节点hadoop2的配置
  4. 03015_JSTL技术
  5. 基础_String
  6. UNIX 系统中 wc 程序的主要部分
  7. day05_06 continue语句、while循环
  8. BZOJ2132 圈地计划 【最小割】
  9. 【VBA】利用Range声明Array(一维/二维)
  10. python 小练习1