【问题标题】:How to access and change Python arrays with logical operators?如何使用逻辑运算符访问和更改 Python 数组?
【发布时间】:2015-06-03 16:18:56
【问题描述】:

我想知道如何在 Python 中执行以下 MATLAB 语句:

X(0.80 < X & X < 1) = 1;

这个声明说:Put 1 in the indices where the value of X is between 0.8 and 1

我正在寻找 Python 列表的 NumPy 数组中的解决方案。

谢谢。

我试过了:

X[X > 0.8 and X < 1]

但它说:ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

【问题讨论】:

    标签: python matlab numpy


    【解决方案1】:

    这是操作顺序的问题,所以只需使用括号:

    X[(X > 0.8) & (X < 1)]
    

    请注意,我还使用 & 号而不是 and

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多