小编sho*_*t71的帖子

Haskell - 重现 numpy 的重塑

进入 Haskell,我试图用列表重现类似numpy 的 reshape 之东西。具体来说,给定一个平面列表,将其重塑为一个 n 维列表:

import numpy as np

a = np.arange(1, 18)
b = a.reshape([-1, 2, 3])

# b = 
# 
# array([[[ 1,  2,  3],
#         [ 4,  5,  6]],
# 
#        [[ 7,  8,  9],
#         [10, 11, 12]],
# 
#        [[13, 14, 15],
#         [16, 17, 18]]])

Run Code Online (Sandbox Code Playgroud)

我能够用固定索引重现行为,例如:

*Main> reshape23 [1..18]
[[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]],[[13,14,15],[16,17,18]]]
Run Code Online (Sandbox Code Playgroud)

我的代码是:

takeWithRemainder :: (Integral n) => n -> [a] -> ([a], [a])
takeWithRemainder _ [] = ([], []) …
Run Code Online (Sandbox Code Playgroud)

arrays haskell numpy

9
推荐指数
2
解决办法
208
查看次数

标签 统计

arrays ×1

haskell ×1

numpy ×1