【问题标题】:Using numpy.nditer in Hy在 Hy 中使用 numpy.nditer
【发布时间】:2017-09-12 03:01:45
【问题描述】:

在python中,下面的代码迭代了一个numpy数组(for循环),numpy数组的值发生了变化:

import numpy
a08_1 = numpy.arange(8).astype(numpy.uint8)
# a08_1: array([0, 1, 2, 3, 4, 5, 6, 7], dtype=uint8)
for x in numpy.nditer(a08_1, op_flags=['readwrite']):
  x[...] = 255 if x == 1 else 0
#
# a08_1: array([  0, 255,   0,   0,   0,   0,   0,   0], dtype=uint8)

是否可以在 Hy 中做类似的事情?我可以使用 (numpy.nditer a08_1) 创建迭代器,但我不知道如何遵循。

谢谢。

【问题讨论】:

    标签: python arrays numpy hy


    【解决方案1】:

    等效的 Hy 如下所示。

    (import numpy)
    (setv a08-1 (-> (numpy.arange 8) (.astype numpy.uint8)))
    (for [x (numpy.nditer a08-1 :op-flags ["readwrite"])]
      (assoc x Ellipsis (if (= x 1) 255 0)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-04
      • 1970-01-01
      相关资源
      最近更新 更多