zoukankan      html  css  js  c++  java
  • numpy中数组的布尔类索引

    第一次看到这样的用法,写几行看一下是怎么用的

    #!/usr/bin/env python
    # coding: utf-8
    
    # In[1]:
    
    
    import numpy as np
    
    
    # In[2]:
    
    
    outdegrees = np.array([1,2,0,3,0,4,0])
    masks = (outdegrees != 0)       
    # 这一步代码是把outdegrees(numpy数组)中为零的位置设为false,非零的地方设为True,产生的是一个布尔型的numpy数组
    # masks = [ True  True False  True False  True False]
    
    current_nodes = np.array([2,1,5,3,6,7,9])
    print(masks)
    print(current_nodes[masks])     # [2 1 3 7]
    # 这里是在current_nodes中只取masks为True的对应位置的元素
    
    
    # In[3]:
    
    
    for i in range(len(outdegrees)):
        # not np.any(masks) masks至少存在一个True为真,全为false则为假
        if not np.any(masks):
            break
        print("1:",i)
    
    masks = np.array([ False,False,False,False,False,False,False])
    for i in range(len(outdegrees)):
        if not np.any(masks):
            break
        print("2:",i)
    
    '''
    1: 0
    1: 1
    1: 2
    1: 3
    1: 4
    1: 5
    1: 6
    '''
    # In[ ]:

      

  • 相关阅读:
    hdu 1686 Oulipo
    [NOI1997] 积木游戏
    错误录——未完待续
    NOI 2014 魔法森林
    hdu 4010 Query on The Trees
    求助大佬6——1种贪心
    51 nod 1205 流水线调度
    bzoj 1180: [CROATIAN2009]OTOCI
    HNOI2010 弹飞绵羊
    SDOI2008 洞穴勘测
  • 原文地址:https://www.cnblogs.com/dong973711/p/14090255.html
Copyright © 2011-2022 走看看