首页 > 程序开发 > Web开发 > Python > 正文
Python Numpy Tutorials: 数组--4
2017-05-13 09:41:00       个评论    来源:Eric2016_Lv的博客  
收藏    我要投稿
# -*- 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]"
点击复制链接 与好友分享!回本站首页
上一篇:Python Numpy Tutorials: 数组--3
下一篇:Python Numpy Tutorials: 数据类型
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做实用的IT技术学习网站