2 条题解
-
0
NUMPY049 题解:打印完整数组
题目
如何打印数组的全部值而不被省略?
解题思路
使用
np.set_printoptions()设置打印选项。代码
import numpy as np np.set_printoptions(threshold=float("inf")) Z = np.zeros((40, 40)) print(Z)代码详解
np.zeros((40, 40))—— 创建 40×40 的大数组np.set_printoptions(threshold=float("inf"))—— 设置阈值为无穷大- 打印数组时不会省略任何元素
set_printoptions 参数
np.set_printoptions( threshold=1000, # 元素超过此数就省略(默认1000) edgeitems=3, # 省略时显示的首尾元素数 linewidth=75, # 每行字符数 precision=8, # 浮点数精度 suppress=False # 是否使用科学计数法 )示例
# 恢复默认 np.set_printoptions(threshold=1000) # 只显示首尾3个元素 np.set_printoptions(edgeitems=3) print(np.arange(20)) # [ 0 1 2 ... 17 18 19]Jupyter 快捷方式
# 在 Jupyter 中可以这样设置 np.set_printoptions(threshold=np.nan, suppress=True)核心知识点
np.set_printoptions()—— 设置打印选项threshold—— 控制省略阈值edgeitems—— 首尾显示元素数
- 1
信息
- ID
- 100
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者