小编Tej*_*jas的帖子

使用list和tuple索引Numpy数组会得到不同的结果?

示例代码:

import numpy as np
a = np.zeros((5,5))
a[[0,1]] = 1     #(list of indices)
print('results with list based indexing\n', a)

a = np.zeros((5,5))
a[(0,1)] = 1   #(tuple of indices)
print('results with tuple based indexing\n',a)
Run Code Online (Sandbox Code Playgroud)

结果:

results with list based indexing
 [[ 1.  1.  1.  1.  1.]
  [ 1.  1.  1.  1.  1.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]
  [ 0.  0.  0.  0.  0.]]

results with tuple based indexing
[[  0.   1.   0.   0. …
Run Code Online (Sandbox Code Playgroud)

arrays indexing numpy python-3.x

5
推荐指数
1
解决办法
80
查看次数

标签 统计

arrays ×1

indexing ×1

numpy ×1

python-3.x ×1