使用List进行NumPy索引编制? [英] NumPy indexing using List?

查看:173
本文介绍了使用List进行NumPy索引编制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SOF,

我在此URL中注意到了一个有趣的NumPy演示:

I noticed an interesting NumPy demo in this URL:

http://cs231n.github.io/python-numpy-tutorial/

我看到了:

import numpy as np

a = np.array([[1,2], [3, 4], [5, 6]])

# An example of integer array indexing.
# The returned array will have shape (3,) and 

print( a[[0, 1, 2], [0, 1, 0]] )
# Prints "[1 4 5]"

我了解使用整数作为索引参数:

I understand using integers as index arguments:

a[1,1]

和此语法:

a[0:2,:]

通常, 如果我使用列表作为索引语法,那是什么意思?

Generally, If I use a list as index syntax, what does that mean?

具体来说, 我不明白为什么:

Specifically, I do not understand why:

print( a[[0, 1, 2], [0, 1, 0]] )
# Prints "[1 4 5]"

推荐答案

最后一条语句将以矩阵表示法打印a(0,0)a(1,1)a(2,0).在a[0][0]a[1][1]a[2][0]的python表示法中.

The last statement will print (in matrix notation) a(0,0), a(1,1) and a(2,0). In python notation that's a[0][0], a[1][1] and a[2][0].

第一个索引列表包含第一个轴的索引(矩阵符号:行索引),第二个列表包含第二个轴的索引(列索引).

The first index list contains the indices for the first axis (matrix notation: row index), the second list contains the indices for the second axis (column index).

这篇关于使用List进行NumPy索引编制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆