python - 输出矩阵用点更新,我想删除数组中的点
问题描述
import numpy as np
x = np.ones((5,5))
print(x)
x[1:-1,1:-1] = 0
print(x)
我得到如下所示的输出:
[[1. 1. 1. 1. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 0. 0. 0. 1.]
[1. 1. 1. 1. 1.]]
解决方案
您可以使用astype
,将其设置为int
:
print(x.astype(int))
结果:
[[1 1 1 1 1]
[1 0 0 0 1]
[1 0 0 0 1]
[1 0 0 0 1]
[1 1 1 1 1]]
推荐阅读
- css - 根据实际定位将 CSS 三角形附加到 MatTooltip?
- javascript - 仅在页面刷新后加载反应应用程序中的样式
- json - 传单 JSON WFS 图层未出现在地图上
- yugabyte-db - 如何在 yugabyte db 中创建具有密码的用户、删除角色和拥有所有权的数据库?
- c - 从不兼容的指针类型警告初始化
- batch-file - 如果该文件夹包含超过 100 个文件,如何仅移动文件夹中的第一个文件?
- javascript - 将 js 变量发布到未以任何方法运行的 PHP
- regex - 查找该行的前 4 个字符应该是“ORA-”,接下来的 5 个字符应该是 Unix 命令/脚本中的数字
- java - 为什么要再次将数组转换为列表到数组
- css - 根据值设置材质 ui 表格单元格