可以给出不不止一维的索引
来源:互联网 发布:东方财富期货软件 编辑:程序博客网 时间:2024/04/29 19:19
我们也可以给出不不止一维的索引,每一维的索引数组必须有相同的形状。
>>> a = arange(12).reshape(3,4)>>> aarray([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])>>> i = array( [ [0,1], # indices for the first dim of a... [1,2] ] )>>> j = array( [ [2,1], # indices for the second dim... [3,3] ] )>>>>>> a[i,j] # i and j must have equal shapearray([[ 2, 5], [ 7, 11]])>>>>>> a[i,2]array([[ 2, 6], [ 6, 10]])>>>>>> a[:,j] # i.e., a[ : , j]array([[[ 2, 1], [ 3, 3]], [[ 6, 5], [ 7, 7]], [[10, 9], [11, 11]]])
自然,我们可以把i和j放到序列中(比如说列表)然后通过list索引。
>>> l = [i,j]>>> a[l] # equivalent to a[i,j]array([[ 2, 5], [ 7, 11]])
然而,我们不能把i和j放在一个数组中,因为这个数组将被解释成索引a的第一维。
>>> s = array( [i,j] )>>> a[s] # not what we want---------------------------------------------------------------------------IndexError Traceback (most recent call last) in ()----> 1 a[s]IndexError: index (3) out of range (0<=index<2) in dimension 0>>>>>> a[tuple(s)] # same as a[i,j]array([[ 2, 5], [ 7, 11]])
>>> a = arange(12).reshape(3,4)
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> i = array( [ [0,1], # indices for the first dim of a
... [1,2] ] )
>>> a[i]
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]])
>>> i.shape
(2, 2)
>>> a[i].shape
(2, 2, 4)
>>> a.shape
(3, 4)
>>> j = array( [ [2,1], # indices for the second dim
... [3,3] ] )
>>> a[i,j]
array([[ 2, 5],
[ 7, 11]])
>>> a[i,2]
array([[ 2, 6],
[ 6, 10]])
>>> a
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
>>> i
array([[0, 1],
[1, 2]])
>>> j
array([[2, 1],
[3, 3]])
>>> a[:,j]
array([[[ 2, 1],
[ 3, 3]],
[[ 6, 5],
[ 7, 7]],
[[10, 9],
[11, 11]]])
>>> l = [i,j]
>>> l
[array([[0, 1],
[1, 2]]), array([[2, 1],
[3, 3]])]
>>> a[l]
array([[ 2, 5],
[ 7, 11]])
>>>
- 可以给出不不止一维的索引
- 给出一个单链表,不知道N的值,怎样遍历一次就可以求出中间结点
- 《收获,不止Oracle》读书笔记(一):索引优化
- 给出一个单链表,不知道节点N的值,怎样只遍历一次就可以求出中间节点,写出算法。
- 给出一个单链表,不知道节点N的值,怎样只遍历一次就可以求出中间节点,写出算法!
- 24.读书笔记收获不止Oracle之 索引的危害
- 谁的人生不迷茫 ?! 迷茫的不止是你!
- 索引的不可视
- 时间不止我们不散
- 给出一个单链表,不知道节点N的值…
- 给出数组(list),生成各种可能的组合,不排序
- 随机产生值不重复的一维数组,并求①最小值,②次小值,③最大值及其索引
- 讓WebBrowser可以辨認Navigate Error Http 404 的錯誤(不止404)
- 宏定义disp可以在给出参数时,在屏幕上显示该字符,不给出参数时,则显示空格
- 【读书笔记】【收获,不止Oracle】索引组织表
- 14.读书笔记收获不止Oracle之 索引
- c++中的#include不止可以这么用
- 鉴别不使用的索引
- 博为峰Java技术文章 ——JavaSE Swing BoxLayout布局管理器I
- Java API文档
- Andorid BlueDroid分析: Semophore的实现分析
- 初探处理器体系结构及寻址模式
- 判断质数与素数表
- 可以给出不不止一维的索引
- 使用IntelliJ IDEA 配置Maven(入门)
- 0119
- 分享一个链接
- 冯·诺依曼结构与哈佛结构浅析
- 20170119C++阶段班05_STL_01inheritance
- 18.C#:Entity Framework添加自动跟踪实体生成器,用来生成表结构对应的标准类
- 静态库的创建和编写makefile
- [Codeforces 496D] Tennis Game (筛法复杂度)