如何使用布尔数组替换Python中的特定索引

时间:2016-05-12 16:48:05

标签: python arrays numpy indexing boolean

我有一个numpy和一个布尔数组:

nparray = [ 12.66  12.75  12.01  13.51  13.67 ]
bool = [ True False False True True ]

我想将nparray中的所有值替换为bool为假的相同值除以3。

我是一名学生,而且我对python索引比较陌生。非常感谢任何建议或建议!

3 个答案:

答案 0 :(得分:1)

命名数组bool可能不是最好的主意。正如ayhan尝试将其重命名为bl或其他内容。

您可以使用numpy.where查看文档here

nparray2 = np.where(bl == False, nparray/3, nparray)

答案 1 :(得分:0)

boolean indexing~一起用作否定运算符:

arr = np.array([12.66, 12.75, 12.01, 13.51, 13.67 ])
bl = np.array([True, False, False, True ,True])

arr[~bl] = arr[~bl] / 3
array([ 12.66      ,   4.25      ,   4.00333333,  13.51      ,  13.67      ])

答案 2 :(得分:0)

只需使用python就可以这样做:

var currentRow = 0;  

 function getAllSelectOptions(){
     var states = { '1': 'Alabama', '2': 'California', '3': 'Florida', 
                   '4': 'Hawaii', '5': 'London', '6': 'Oxford' };

      return states;

    }
            validateText = function (value, colname) {
         // do validations if any and return false               
              return [true];
            };

            "use strict";
            var mydata = [
                    {id:"1", DocGroupName: "2", DocList: "y", Mandatory: "z"},
                    {id:"2", DocGroupName: "6", DocList: "y", Mandatory: "z"},
                ];
            $("#list").jqGrid({


                //url:'php.scripts/customers.get.php',
                //datatype: 'xml',
                //mtype: 'POST',
                datatype: "local",
                data: mydata,
                height: "auto",
              colNames: ['id', 'Document Group Name','Document Name','No of Mandatory'],
              colModel :[
                    {name:'id', index:'id', width:55},
                    {name:'DocGroupName', index:'DocGroupName', width:90, editable: true,edittype: 'select', 
                                            formatter: 'select',

                                            editoptions:{value: getAllSelectOptions()}, "editrules":{"custom":true,"custom_func":validateText}},
                    {name:'DocList', index:'DocList', width:90, editable: true },
                    {name:'Mandatory', index:'Mandatory', width:90, editable: true}

                ],
                pager: '#pager',
                rowNum:10,
                rowList:[10,20,30],
                sortname: 'idcustomers',
                sortorder: 'asc',
                viewrecords: true,
                gridview: true,
                caption: 'Customers',
                cellEdit: true,
                cellsubmit: 'clientArray',
                afterSaveCell: function(rowid,name,val,iRow,iCol) {
                if(name=='DocGroupName')
                 {
                     var row = $('#list').jqGrid('getRowData',currentRow);      
                     row.DocList='';
                     $('#list').jqGrid('setRowData',currentRow,row);       
                 }                  
                },
                beforeSaveCell: function(rowid,name,val,iRow,iCol) {
                 // var row = $("#list").getRowData(rowid);
                   var row = $('#list').jqGrid('getRowData',rowid);
                   currentRow= rowid;

                },

 });

结果是:

nparray = [12.66, 12.75, 12.01, 13.51, 13.67]
bool = [True, False, False, True, True]
map(lambda x, y: x if y else x/3.0, nparray, bool)