[英]appending and formatting multi dimensional arrays in python Numpy
I want to write a code that appends a value to the order
multidimensional array.我想编写一个将值附加到
order
多维数组的代码。 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).如果最后一列是
0
序indx[-1:,1] (function for the last element in the first column)
,它将 append 10000
到第二列以及1
在第一列(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).如果第一列最后一个元素为
1
,则第一列中的 append 2
和第二列中的20000
(2, 20000)。 How could i write such code without the use of a for loop or list comprehensions.如果不使用 for 循环或列表推导,我怎么能编写这样的代码。
import numpy as np
order = np.array([[ 0, 38846],
[ 1, 51599],
[ 0, 51599],
[ 1, 52598],
[ 0, 290480],
[ 1, 335368],
[ 0, 335916]])
Expected Output预计 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.