欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python Numpy Tutorials: 数组--4

程序员文章站 2023-11-22 23:58:16
# -*- coding: utf-8 -*- """ python version: 3.5 created on thu may 11...
# -*- coding: utf-8 -*-
"""
python version: 3.5
created on thu may 11 14:42:49 2017
e-mail: eric2014_lv@sjtu.edu.cn
@author: didilv
"""

import numpy as np

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

# 寻找元素大于2的值,并且返回其bool值
bool_idx = (a > 2)  # find the elements of a that are bigger than 2;
                    # this returns a numpy array of booleans of the same
                    # shape as a, where each slot of bool_idx tells
                    # whether that element of a is > 2.

print(bool_idx)      # prints "[[false false]
                    #          [ true  true]
                    #          [ true  true]]"

# we use boolean array indexing to construct a rank 1 array
# consisting of the elements of a corresponding to the true values
# of bool_idx
print(a[bool_idx])  # prints "[3 4 5 6]"

# we can do all of the above in a single concise statement:
print(a[a > 2]+10)     # prints "[13 14 15 16]"