【问题标题】:Error when passing a Variable to an np.where function将变量传递给 np.where 函数时出错
【发布时间】:2020-08-17 17:10:08
【问题描述】:

我有读取和显示 SEGY 地震数据的工作代码。功能版本太大,无法在此处粘贴。我遇到的问题在于以下部分。

    #find traces indices that belong to Inline x
    CXline=self.Line_box.text() #Get chosen line from Gui textbox
    if CXline=="": #If empty, use all inline indices
        inds=np.where(inlines)[0]
    else:    
        inds=np.where(inlines==(CXline))[0] #load data for specific inline indices.

如果 Line_box(QT 文本编辑框)为空,“if”部分按预期工作,“inds”输出数组正确,代码继续运行。

如果我在 Line_box 中输入一个数字(例如 425),我希望在 np.where 函数中使用该数字但它不起作用,它只是输出一个空数组并且代码停止.我还收到以下消息(尽管它是导致崩溃的空数组)。

Warning (from warnings module):
File "/Users/willhutchins/Documents- Offline/Python/Synth_Seismic/SEGY View/SEGY_Viewer.py", line 269
inds=np.where(inlines==(str(CXline)))[0]
FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison

如果我手动在代码中键入一个数字,代码会按预期运行,因此这肯定与 np.where 如何理解来自 Line_box 的输入有关。我假设我的语法错误或需要将 Line_box 输入转换为不同的 dtype。

        else:    
        inds=np.where(inlines==425)[0] 

【问题讨论】:

  • where 只返回其参数的真/非零元素的索引。因此,您必须首先获得inlines==425 或任何正确的信息。解释一下inlinesCXline 是什么。

标签: python macos numpy where-clause


【解决方案1】:

新手错误(漫长的一天)。这是一个 dtype 冲突。我只需要将传入的 Line_box 字符串转换为整数。

        else:    
        inds=np.where(inlines==(int(CXline)))[0]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 2012-02-11
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多