簡體   English   中英

在 python Numpy 中附加和格式化多維 arrays

[英]appending and formatting multi dimensional arrays in python Numpy

我想編寫一個將值附加到order多維數組的代碼。 如果最后一列是0indx[-1:,1] (function for the last element in the first column) ,它將 append 10000到第二列以及1在第一列(1、10000)。 如果第一列最后一個元素為1 ,則第一列中的 append 2和第二列中的20000 (2, 20000)。 如果不使用 for 循環或列表推導,我怎么能編寫這樣的代碼。

import numpy as np

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

預計 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM