0

I am trying to make a function where if the last value of order is 1 then the code will append 1 to orders and then append the number 20 to the value array as well. If the last value is 0 in order then it will append 1 to order and append the number 15 to value.

import numpy as np

order = np.array([0,1,0,0,0])
order1 = np.array([0,1,0,0,1])
value= np.array([10,55,30,3,10])

Expected Output:

#if last element is equal to 1 
order = np.array([0,1,0,0,0,1])
value= np.array([10,55,30,3,10,20])

#if last element is equal to 0 
order1 = np.array([0,1,0,0,1,0])
value= np.array([10,55,30,3,10,15])

2 Answers 2

1
def extend_order_and_value(order, value, order_0_extensors = (1, 20), order_1_extensors = (0, 15)):
    ## Return order and value extended by the values given in
    ## order_0_extensors or order_1_extensors depending on last order's value.
    ## the extensors have the form: (order_value, values_value) for the cases
    ## 0 or 1 as last value in order.
    
    # ensure that order and value are numpy arrays!
    order, value = np.array(order), np.array(value)
    
    if order[-1] == 0:
        order_val, value_val = order_0_extensors
    elif order[-1] == 1:
        order_val, values_val = order_1_extensors
    else:
        return order, value # return unchanged or raise error
    return np.concatenate([order, np.array([order_val])]), np.concatenate([values, np.array([values_val])])

Use it like this:

import numpy as np

order = np.array([0,1,0,0,0])
order1 = np.array([0,1,0,0,1])
value= np.array([10,55,30,3,10])


new_order, new_value = extend_order_and_value(order, value)                                                                
new_order
## array([0, 1, 0, 0, 0, 1])
new_value
## array([10, 55, 30,  3, 10, 20])

new_order1, new_value1 = extend_order_and_value(order1, value)                                                               
new_order1
## array([0, 1, 0, 0, 1, 0])
new_value1
## array([10, 55, 30,  3, 10, 15]))

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you it works I want to do the same manor with 2 dimensional objects I would appreciate it if you could take a, look at this issue: stackoverflow.com/questions/66201608/…
1

Using if-else

import numpy as np

order = np.array([0,1,0,0,0])
order1 = np.array([0,1,0,0,1])
value= np.array([10,55,30,3,10])

to_be_appended = None

if order[-1] == 0:
    to_be_appended = 1
    value = np.append(value, 15)
elif order[-1] == 1:
    to_be_appended = 0
    value = np.append(value, 20)

order = np.append(order, to_be_appended)

print(order, value)

NOTE: If append id required, then I think list should be used since np.append will create a new array everytime.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.