【问题标题】:Extract a row from a Python numpy array by condition按条件从 Python numpy 数组中提取一行
【发布时间】:2023-01-31 20:21:17
【问题描述】:

我有一个数组(称为“吸引子”),如下所示:

[['0000000000' '0.0' '0.0']
 ['0000000001' '0.0' '1.0']
 ['0000000010' '0.0' '2.0']
...........................

我想创建一个新数组,其中包含原始数组中第三列为 0 的所有行。我尝试以下操作:

print(attractors[attractors[: , 2] == 0][: , 0])

但我收到以下错误:

            json export to visualize state transition diagram with compression
 - tests.py:247: FutureWarning: elementwise comparison failed; 
    returning scalar instead, but in the 
        future will perform elementwise comparison 
              print(attractors[attractors[: , 2] == 0][: , 0])

如果我在条件上加上括号,就像这样:

print(attractors[attractors[: , 2] == "0"][: , 0])

然后错误没有出现,但是结果不是我预期的(只有空括号[])

【问题讨论】:

    标签: python numpy-ndarray


    【解决方案1】:

    您可以使用列表理解:

    [x for x in attractors if x[2] == 0]
    

    如果它是一个字符串,您可以将相等性更改为== '0.0'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      相关资源
      最近更新 更多