Python Numpy Tutorials: 数组--4

来源:互联网 发布:淘宝名字叫什么好 编辑:程序博客网 时间:2024/06/17 04:56
# -*- coding: utf-8 -*-"""Python Version: 3.5Created on Thu May 11 14:42:49 2017E-mail: Eric2014_Lv@sjtu.edu.cn@author: DidiLv"""import numpy as npa = 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_idxprint(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]"
0 0
原创粉丝点击