gpt4 book ai didi

python - 在 Hy 中使用 numpy.nditer

转载 作者:行者123 更新时间:2023-12-01 02:36:25 26 4
gpt4 key购买 nike

在 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) 创建迭代器,但我不知道如何遵循。

谢谢。

最佳答案

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

关于python - 在 Hy 中使用 numpy.nditer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46167222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com