简体   繁体   中英

appending and formatting multi dimensional arrays in python Numpy

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]]

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM