Home > Net >  Python: check a condition and print the row of an array
Python: check a condition and print the row of an array

Time:01-21

I would like to check a condition in an array and if the condition is True. I want to print the rows of the array. And if the condition is False. I want to print the another set of rows in the array. Example

A1 = np.array(

[[ 54.          15.           1.           0.        ]
 [ 55.          13.66311896   1.           1.33688104]
 [ 56.          10.16311896   1.95491503   4.93024146]
 [ 57.           5.83688104   4.45491503   9.79281302]
 [ 58.           2.33688104   7.54508497  14.25456836]
 [ 59.           1.          10.04508497  16.66774016]
 [ 60.           1.          11.          17.20465053]
 [ 61.           1.47745751  11.          16.81841715]
 [ 62.           2.72745751  10.04508497  15.24561777]
 [ 63.           4.27254249   7.54508497  12.56648248]
 [ 64.           5.52254249   4.45491503  10.0875487 ]
 [ 80.           4.92705098   2.33688104  10.16127712]
 [ 81.           3.07294902   5.83688104  12.8705075 ]
 [ 82.           1.57294902  10.16311896  16.25572045]
 [ 83.           1.          13.66311896  18.87735632]
 [ 84.           1.          15.          19.79898987]]

Now, I want to check if the value of elements in the 3rd column are increasing than previous element (i.e, if in 3rd column i 1 > i) then the all the increasing elements data should be grouped in 1 array. If the condition is False. the remainders has to be grouped into another array.

That means Condition: In the 3rd col of A1 0 > 1.33688104 > 4.93024146 > 9.79281302 > 14.25456836 > 16.66774016 > 17.20465053 > 18.87735632 > 19.79898987 therefore output should be

Array 1 = [ 54.          15.           1.           0.        ]
 [ 55.          13.66311896   1.           1.33688104]
 [ 56.          10.16311896   1.95491503   4.93024146]
 [ 57.           5.83688104   4.45491503   9.79281302]
 [ 58.           2.33688104   7.54508497  14.25456836]
 [ 59.           1.          10.04508497  16.66774016]
 [ 60.           1.          11.          17.20465053]
 [83.           1.          13.66311896  18.87735632]
 [ 84.           1.          15.          19.79898987]

Array 2:

[ 61.           1.47745751  11.          16.81841715]
 [ 62.           2.72745751  10.04508497  15.24561777]
 [ 63.           4.27254249   7.54508497  12.56648248]
 [ 64.           5.52254249   4.45491503  10.0875487 ]
 [ 80.           4.92705098   2.33688104  10.16127712]
 [ 81.           3.07294902   5.83688104  12.8705075 ]
 [ 82.           1.57294902  10.16311896  16.25572045]

And I want to repeat this loop until single value remained in the array.

My code

pre = 0
list1 = [] # Residual
list2 = [] # part of increasing 3rd row

for index in A1[:,3]:
    a = index > pre
    if a == False:          
                            
        list1.append(index) # Increasing 3rd row             
    else:
        list2.append(index) # other remainin values
        pre = index

I tried doing this, but I can able to print only last row as a list. Please help me printing entire row in the array in python.

CodePudding user response:

It looks like you may be misinterpreting the parameters of Python loops. While your loop is correct for comparing the array values, this implementation makes it more difficult to append the desired rows to the new lists you wish to create.

ie looping through A1[:,3] means that your "index" value is simply the values found in the third column, while if you were to loop through A1 you'd find that the "index" variable is now the entire row and can be appended to your final lists relatively easily; however, Python also allows you to use the range() function to loop through a set of integers as I have done in my example. This allows Python's loops to act a bit closer to what you might expect in some other languages. In my code I have used the range() function, but you could just as easily adapt this code to loop through the rows of A1 rather than the index values.

Another small note: You appear to be using a numpy array as your input, but are then using lists within your function. I typically try to stick to one kind of array, so that you can reliably use the same methods between all your arrays. In this instance it wont be too much of a hastle, but just something to watch out for in the future.

Here is my code sample:

pre = A1[0][3]
list1 = [] # Increasing Row 3
list1.append(A1[0])
list2 = [] # Residual

for index in range(1,len(A1)):
    if (A1[index][3] > pre):        
        list1.append(A1[index]) # Increasing 3rd row
        pre = A1[index][3]       
    else:
        list2.append(A1[index]) # Residual values

print("List 1 (Increasing Values):")
for row in list1:
    print(row)
print("\nList 2 (Non-Increasing Values):")
for row in list2:
    print(row)

You'll see that I also adjusted the initial values of the your "pre" and "list1" variables, so to use the first row as your starting point for these values. Otherwise you might find the code a little rigid when using different array inputs such as with an initially negative number. (Also in this case where your first row's value is equal to that of the initial pre value)

CodePudding user response:

This should do it, check the output below to see if it is what you wanted:

def your_function(a, index=3):

  # create the array to return: a list of slices,
  # where the third column always contains ascending elements
  array_output_slices = list()
  # work on the remaining slice (initially set to the whole array)
  reminder = a
  iteration = 1
  # until nothing remains (i.e. we processed the whole array)
  while not reminder.size == 0:
    index_column = reminder[:, index]
    # get the index of the last element in ascending order
    max_ordered_index = 0
    while max_ordered_index   1 < len(index_column) and index_column[max_ordered_index   1] > index_column[max_ordered_index] :
      max_ordered_index  = 1
    # get a slice and the remainder
    output_slice = reminder[:max_ordered_index   1]
    reminder = reminder[max_ordered_index   1:]
    print(f"A{iteration}:\n", output_slice, "\n") 
    print(f"Reminder:\n", reminder, "\n\n--------------------------------------------------\n\n")

    # append the slice to the list of slices to output
    array_output_slices.append(output_slice)
    iteration  = 1

  return array_output_slices

With your array as input it prints:

A1:
 [[54.         15.          1.          0.        ]
 [55.         13.66311896  1.          1.33688104]
 [56.         10.16311896  1.95491503  4.93024146]
 [57.          5.83688104  4.45491503  9.79281302]
 [58.          2.33688104  7.54508497 14.25456836]
 [59.          1.         10.04508497 16.66774016]
 [60.          1.         11.         17.20465053]] 

Reminder:
 [[61.          1.47745751 11.         16.81841715]
 [62.          2.72745751 10.04508497 15.24561777]
 [63.          4.27254249  7.54508497 12.56648248]
 [64.          5.52254249  4.45491503 10.0875487 ]
 [80.          4.92705098  2.33688104 10.16127712]
 [81.          3.07294902  5.83688104 12.8705075 ]
 [82.          1.57294902 10.16311896 16.25572045]
 [83.          1.         13.66311896 18.87735632]
 [84.          1.         15.         19.79898987]] 

--------------------------------------------------


A2:
 [[61.          1.47745751 11.         16.81841715]] 

Reminder:
 [[62.          2.72745751 10.04508497 15.24561777]
 [63.          4.27254249  7.54508497 12.56648248]
 [64.          5.52254249  4.45491503 10.0875487 ]
 [80.          4.92705098  2.33688104 10.16127712]
 [81.          3.07294902  5.83688104 12.8705075 ]
 [82.          1.57294902 10.16311896 16.25572045]
 [83.          1.         13.66311896 18.87735632]
 [84.          1.         15.         19.79898987]] 

--------------------------------------------------


A3:
 [[62.          2.72745751 10.04508497 15.24561777]] 

Reminder:
 [[63.          4.27254249  7.54508497 12.56648248]
 [64.          5.52254249  4.45491503 10.0875487 ]
 [80.          4.92705098  2.33688104 10.16127712]
 [81.          3.07294902  5.83688104 12.8705075 ]
 [82.          1.57294902 10.16311896 16.25572045]
 [83.          1.         13.66311896 18.87735632]
 [84.          1.         15.         19.79898987]] 

--------------------------------------------------


A4:
 [[63.          4.27254249  7.54508497 12.56648248]] 

Reminder:
 [[64.          5.52254249  4.45491503 10.0875487 ]
 [80.          4.92705098  2.33688104 10.16127712]
 [81.          3.07294902  5.83688104 12.8705075 ]
 [82.          1.57294902 10.16311896 16.25572045]
 [83.          1.         13.66311896 18.87735632]
 [84.          1.         15.         19.79898987]] 

--------------------------------------------------


A5:
 [[64.          5.52254249  4.45491503 10.0875487 ]
 [80.          4.92705098  2.33688104 10.16127712]
 [81.          3.07294902  5.83688104 12.8705075 ]
 [82.          1.57294902 10.16311896 16.25572045]
 [83.          1.         13.66311896 18.87735632]
 [84.          1.         15.         19.79898987]] 

Reminder:
 [] 

--------------------------------------------------
  •  Tags:  
  • Related