使用纽比.nditer在Hy

2024-09-28 21:01:23 发布

您现在位置:Python中文网/ 问答频道 /正文

在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也可以这样吗?我可以用(纽比.nditer但我不知道该怎么做。在

谢谢。在


Tags: 代码inimportnumpyfor数组arrayflags
1条回答
网友
1楼 · 发布于 2024-09-28 21:01:23

在 等效的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)))

相关问题 更多 >