【问题标题】:Replacing the values of an "L" shape section of a Python 2D numpy array替换 Python 2D numpy 数组的“L”形部分的值
【发布时间】:2021-03-15 19:54:21
【问题描述】:

假设我有以下二维数组:

[[a, a, a, a],
 [a, a, b, a],
 [a, a, b, a],
 [a, c, c, a],
 [a, a, a, a]]

我想把它改成下面这样:

[[a, a, a, a],
 [a, a, d, a],
 [a, a, d, a],
 [a, d, d, a],
 [a, a, a, a]]

我目前分两步进行:例如 arr[1:4,2]=d 和 arr[3, 1:3]=d

有没有一种方法可以一步完成,而且不会影响性能?

【问题讨论】:

  • 你可以在python的一行中为变量赋值:a = b = 5。这能回答你的问题吗?
  • arr[1:4,2]=d 这不是用于列表的有效 Python 语法。你在使用像 numpy 这样的东西吗?
  • 是的,我应该提到我正在使用 numpy 数组。我只是想知道是否有一种方法可以选择我希望在一个作业中更改的区域,而不是做两个单独的作业。

标签: python arrays python-3.x numpy


【解决方案1】:

可能是不可能的,你不能在切片上得到L形,至少需要2个切片才能形成一个L。

【讨论】:

    猜你喜欢
    • 2014-10-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 2016-06-13
    • 2013-09-11
    • 2022-01-07
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多