Python 多维数组 - 在每行的开头和结尾添加附加值

发布于 2024-10-08 00:43:01 字数 335 浏览 0 评论 0原文

如何在多维数组的每一行的开头和结尾添加 0?这是我试图应用于每一行的函数。

def zero(ltr):
  for x in range (1,int((N+1)/2)):
        ltr = append(([0]), ltr)
        ltr = append(ltr,([0]))
  return ltr 

我尝试过同时使用

for row in a:
   zero(row)

apply_along_axis(零,1,a) 这些命令都没有达到我想要的效果。

How do I add 0's to the beginning and end of each row of a multidimensional array? This is the function I am trying to apply to each row.

def zero(ltr):
  for x in range (1,int((N+1)/2)):
        ltr = append(([0]), ltr)
        ltr = append(ltr,([0]))
  return ltr 

I have tried using both

for row in a:
   zero(row)

and
apply_along_axis(zero,1,a)
Neither one of these commands does what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把人绕傻吧 2024-10-15 00:43:01

无法将条目添加到二维数组的单行中。所有行必须始终具有相同的长度。但您可以一次向所有行添加条目。

如果 a 是二维 NumPy 数组,则可以使用 numpy.hstack 在左侧和右侧添加零:

a = numpy.array([[  0.,   1.,   2.,   3.],
                 [  4.,   5.,   6.,   7.],
                 [  8.,   9.,  10.,  11.]])
numpy.hstack((numpy.zeros((a.shape[0], 2)), a, numpy.zeros((a.shape[0], 1))))
# array([[  0.,   0.,   0.,   1.,   2.,   3.,   0.],
#        [  0.,   0.,   4.,   5.,   6.,   7.,   0.],
#        [  0.,   0.,   8.,   9.,  10.,  11.,   0.]])

为了举例,我添加了 左侧有 2 个零,右侧有 1 个零。

It is not possible to add entries to single rows of a two-dimensional array. All rows must always have the same length. But you can add entries to all rows at once.

If a is a two-dimensional NumPy array, you can use numpy.hstack to add zeros to left and the right:

a = numpy.array([[  0.,   1.,   2.,   3.],
                 [  4.,   5.,   6.,   7.],
                 [  8.,   9.,  10.,  11.]])
numpy.hstack((numpy.zeros((a.shape[0], 2)), a, numpy.zeros((a.shape[0], 1))))
# array([[  0.,   0.,   0.,   1.,   2.,   3.,   0.],
#        [  0.,   0.,   4.,   5.,   6.,   7.,   0.],
#        [  0.,   0.,   8.,   9.,  10.,  11.,   0.]])

For the sake of example, I added 2 zeros to the left and 1 zero to the right.

莫言歌 2024-10-15 00:43:01

编辑:我发现您已经在使用numpy。为了教育起见,我将保留此内容,但您应该使用 hstack ,如 斯文的回答

>>> a = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
>>> for row in a:
...     row.insert(0, 0)
...     row.append(0)
...
>>> a
[[0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0]]

或者如果您愿意:

>>> import operator
>>> a = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
>>> map(operator.methodcaller("insert", 0, 0), a)
[None, None, None]
>>> map(operator.methodcaller("append", 0), a)
[None, None, None]
>>> a
[[0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0]]

EDIT: I see you're already using numpy. I'll leave this for the sake of education, but you should use hstack as in Sven's answer.

>>> a = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
>>> for row in a:
...     row.insert(0, 0)
...     row.append(0)
...
>>> a
[[0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0]]

or if you prefer:

>>> import operator
>>> a = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
>>> map(operator.methodcaller("insert", 0, 0), a)
[None, None, None]
>>> map(operator.methodcaller("append", 0), a)
[None, None, None]
>>> a
[[0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文