【问题标题】:I want to append 1st element of numpy array with 1st element in another numpy array and do this for all other elements我想将 numpy 数组的第一个元素与另一个 numpy 数组中的第一个元素附加,并对所有其他元素执行此操作
【发布时间】:2020-10-02 09:23:04
【问题描述】:

我有三个 numpy 数组

X = [1,2,3,4,4,5,56,..,n]
Y = [1,2,344,4,4,4,..,n]
Z = [1,2,244,24,445,64,..,n]

我想做这样的输出

final_list = [(X1,Y1,Z1),(X2,Y2,Z2),(X3,Y3,Z3), ... (Xn,Yn,Zn)]

然后检查其中任何一个中的 Z 是否 > 某个阈值 弹出所有对应的 X 和 Y

请问有什么建议吗? 我试过了 np.conctatenate 但没有任何好的结果。

非常感谢:)

【问题讨论】:

    标签: arrays python-3.x list numpy append


    【解决方案1】:

    if else 的简单方法是:

    X = [1,2,3,4,4,5,56]
    Y = [1,2,344,4,4,4,89]
    Z = [1,2,244,24,445,64,89]
    d=[]
    for i in range(len(X)):
        if Z[i]>thresh:
            print("print something")
        else:
            d.append([X[i],Y[i],Z[i]])
    print(d)
    

    如果您在创建列表时检查 z>thresh 是否不需要稍后弹出这些项目。

    【讨论】:

    • 非常感谢@Jadhav,它工作得很好。并且有没有办法将它们返回,因为我想在 3D 中可视化它们,最好让它们像 x 和 y 和 z 的所有点一样,因为我正在使用方法 ax = fig.add_subplot(111, projection = ' 3d') ax.scatter(Xs, Ys, Zs, c = 'r', marker = "o") plt.show()
    【解决方案2】:

    只有列表理解的一种方法:

    out = [(x,y,z) for x,y,z in zip(X,Y,Z) if z<threshold]
    

    使用 numpy,您可以执行以下操作:

    xyz = np.array([X,Y,Z])
    
    under_thresh = xyz[xyz[-1]<threshold]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-07
      • 2022-11-16
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 2022-12-07
      • 1970-01-01
      相关资源
      最近更新 更多