【问题标题】:how can I to put the value of temp at the end of 3-Dimension list? am I right?如何将 temp 的值放在 3 维列表的末尾?我对吗?
【发布时间】:2019-12-17 09:42:02
【问题描述】:

currentStep 形状为 (1, 20, 1)

就像这张图片,我想在currentStep最后一个位置插入临时值并删除第一个位置(其中20个对应部分)

如何将 temp 的值放在 currentStep 的末尾

表达...[:, I want to put temp value in end of here, and remove first position :]

我试过了

currentStep = currentStep[:,1:,:]
currentStep[:,-1:,:] = temp

此代码是删除我要插入临时元素的最后一个元素

【问题讨论】:

    标签: python list numpy


    【解决方案1】:

    基于this answer,您可以使用np.deletenp.append 的组合:

    In [20]: inner = [[i] for i in range(20)]
    
    In [21]: arr = np.array([inner])
    
    In [22]: arr.shape
    Out[22]: (1, 20, 1)
    
    In [23]: arr = np.delete(arr, 0, 1)
    
    In [24]: arr.shape
    Out[24]: (1, 19, 1)
    
    In [61]: np.append(arr[0], [[12]],axis=0)
    Out[61]:array([[ 1],[ 2], [ 3], [ 4], [ 5], [ 6], [ 7], [ 8], [ 9],
                   [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [12]])
    

    把它放在开头使用numpy.insert:

    np.insert(arr2, 0, 12,1)
    

    【讨论】:

    • 这就是我想要的,但是,我想要 12 到最后一个位置 np.insert(currentStep, -1, 12, 1) 不起作用....我试过像这样 imgur.com/6llKlgZ 12 不会到最后一个位置
    • 已更新以回答您的实际问题。请注意,我在 append 中使用了 axis 作为关键字参数,我无法让 np.append(arr[0], [[12]], 0) 工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多