Search code examples
pythonscilab

How to select list elements based on crteria from other lists


I am new to Python, coming from SciLab (an open source MatLab ersatz), which I am using as a toolbox for my analyses (test data analysis, reliability, acoustics, ...); I am definitely not a computer science lad. I have data in the form of lists of same length (vectors of same size in SciLab). I use some of them as parameter in order to select data from another one; e.g.

t_v = [1:10]; // a parameter vector  
p_v = [20:29]; another parameter vector 
res_v(t_v > 5 & p_v < 28); // are the res_v vector elements of which "corresponding" p_v and t_v values comply with my criteria; i can use it for analyses. 

This is very direct and simple in SciLab; I did not find the way to achieve the same with Python, either "Pythonically" or simply translated. Any idea that could help me, please? Have a nice day, Patrick.


Solution

  • You could use numpy arrays. It's easy:

    import numpy as np                                                                                                                                                                            
                                                                                                                                                                                                  
    par1 = np.array([1,1,5,5,5,1,1])                                                                                                                                                              
    par2 = np.array([-1,1,1,-1,1,1,1])                                                                                                                                                            
    data = np.array([1,2,3,4,5,6,7])                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                                
    print(par1)                                                                                                                                                                                   
    print(par2)                                                                                                                                                                                   
    print(data)  
                                                                                                                                                                             
    bool_filter = (par1[:]>1) & (par2[:]<0)
    # example to do it directly in the array                                                                                                                                                                                    
    filtered_data = data[ par1[:]>1 ]                                                                                                                                                             
    print( filtered_data )                                                                                                                                                                        
    #filtering with the two parameters                                                                                                                                                                                              
    filtered_data_twice = data[ bool_filter==True ]                                                                                                                                                       
    print( filtered_data_twice )  
    

    output:

    [1 1 5 5 5 1 1]
    [-1  1  1 -1  1  1  1]
    [1 2 3 4 5 6 7]
    [3 4 5]
    [4]
                                   
    

    Note that it does not keep the same number of elements.