0

I want to write a code that appends a value to the order multidimensional array. If the last column is 0 order indx[-1:,1] (function for the last element in the first column) the it will append 10000 to the second column as well as 1 on the first column (1, 10000). If the first column last element is 1 than it will append 2 in the first column and 20000 in the second column (2, 20000). How could i write such code without the use of a for loop or list comprehensions.

import numpy as np

order = np.array([[     0,  38846],
                  [     1,  51599],
                  [     0,  51599],
                  [     1,  52598],
                  [     0, 290480],
                  [     1, 335368],
                  [     0, 335916]])

Expected Output

#if the last element on column 1 is 1
[[     0,  38846]
 [     1,  51599]
 [     0,  51599]
 [     1,  52598]
 [     0, 290480]
 [     1, 335368]
 [     0, 335916]
 [     2,  20000]]
#if the last element on column 1 is 0
[[     0  38846]
 [     1  51599]
 [     0  51599]
 [     1  52598]
 [     0 290480]
 [     1 335368]
 [     0 335916]
 [     1  10000]]

2
  • 1
    Write the necessary code with lists or what ever you know best, and wel'll suggest inprovements. Get something working first and worry about fast later. Commented Feb 15, 2021 at 1:36
  • 1
    You can use np.vstack to add the desired row. Commented Feb 15, 2021 at 2:00

1 Answer 1

1
def extend(order):
    if order[-1, 0] == 0:
        return np.concatenate([order, np.array([[1, 10000]])], axis=0)
    elif order [-1, 0] == 1:
        return np.concatenate([order, np.array([[2, 20000]])], axis=0)
Sign up to request clarification or add additional context in comments.

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.